首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在春季只使用@scope(" prototype )注释来制作bean原型?

如何在春季只使用@scope(" prototype )注释来制作bean原型?
EN

Stack Overflow用户
提问于 2018-06-10 14:58:20
回答 4查看 2.1K关注 0票数 2

我有这个主修班

代码语言:javascript
复制
public class Draw {
    public static void main(String[] args) {
        ApplicationContext  context = new ClassPathXmlApplicationContext("spring.xml");

        Triangle t = (Triangle) context.getBean("triangle");
        Triangle t1 = (Triangle) context.getBean("triangle");

        t.show();

        System.out.println(t == t1);

    }
}

三角形类

代码语言:javascript
复制
@Service
@Scope("prototype")
public class Triangle {
    private Point pointa;

    public Point getPointa() {
        return pointa;
    }

    public Triangle(Point pointa) {
        this.pointa = pointa;
    }

    public void show() {
        System.out.println("POINT A (" + pointa.getX() + "," + pointa.getY() + ")");

    }

    @Override
    public String toString() {
        return "Triangle [pointa=" + pointa + "]";
    }
}

和SPRING.XML

代码语言:javascript
复制
<context:component-scan base-package="com.spring.demo" />
<mvc:annotation-driven />
<context:component-scan base-package="com.spring.demo" />
<bean id="triangle" class="com.spring.demo.Triangle" autowire="constructor">
</bean>

<bean id="pointabc" class="com.spring.demo.Point">
    <property name="x" value="0" />
    <property name="y" value="0" />
</bean>

<context:annotation-config />

我知道为了制作一个bean原型,我们在scope=中使用了spring.xml中的“prototype”。但是我只想使用@scope("prototype")来制作bean原型。我不知道为什么上面的代码不能正常工作。我做了很多研究,但没有找到任何答案

代码语言:javascript
复制
System.out.println(t == t1); this gives me true while ideally it should be false.
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-06-10 15:49:08

由于ApplicationContext有几个实现,所以XML配置使用了XML。

通过XML方式,您只需在bean元素中设置作用域属性。

AnnotationConfigApplicationContext是ApplicationContext的实现,用于基于Java的配置手段,用于基于注释的配置,如@Bean等。

如果您想使用注释方式,那么您需要定义基于Java的配置,并且需要使用AnnotationConfigApplicationContext对象。

代码语言:javascript
复制
@Configuration
public class AppConfiguration{
   @Bean 
   @Scope("prototype")
   public Triangle triangle(){
      return new Triangle();
   }
}

然后得到豆子如下所示。

代码语言:javascript
复制
  ApplicationContext ctx = 
         new AnnotationConfigApplicationContext(AppConfiguration.class);

      Triangle triangle= ctx.getBean(Triangle.class);
票数 0
EN

Stack Overflow用户

发布于 2018-06-11 08:24:09

Triangle声明为prototype没有效果,因为它在spring.xml中实例化为单例。

关键是每次您需要一个新的原型bean时都要一个新的原型bean。

这可以通过使用配置类(并从<bean id="triangle" ...中删除spring.xml )来实现。

代码语言:javascript
复制
package com.spring.demo;

@Configuration
public class MyProvider {

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Triangle create() {
        return new Triangle();
    }

}

用法如下:

代码语言:javascript
复制
package com.spring.demo;

@Component
public class MySingleton {

    @Autowired
    private MyProvider myProvider;

    @PostConstruct // will execute this method once app context loaded    
    public void doStuffThatNeedsAPrototypeBeanInstance() {
        Triangle t1 = myProvider.create();
        Triangle t2 = myProvider.create();
        ...
    }
}

添加@PostConstruct的效果是,一旦初始化了应用程序上下文,就会调用该方法,以替代在main方法中运行东西。这样,执行就在spring中,通过注释轻松地访问其他spring。

票数 1
EN

Stack Overflow用户

发布于 2018-06-13 15:54:59

对于注释配置,我们使用AnnotationConfigApplicationContext --抽象 Application Context的实现

代码语言:javascript
复制
public class Scope{

    public static void main(String[] args) {
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(TeacherConfig.class);
    Teacher tea=(Teacher) context.getBean("teacher");
    Teacher tea1=(Teacher) context.getBean("teacher");
    System.out.println(tea==tea1);
        }

}

教师班

代码语言:javascript
复制
public class Teacher {
public void print(String msg){
    System.out.println("TEACHER -->"+msg);
}

TeacherConfig

代码语言:javascript
复制
@Configuration
public class TeacherConfig {

    @Bean(name = "teacher")
    @Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)
    public Teacher Teacher() {
        return new Teacher();
    }
}

@Configuration告诉Spring这是与spring.xml等价的核心Spring配置文件,并通过@ bean .定义Bean

System.out.println(tea==tea1);的输出为

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50784972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档