我有这个主修班
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);
}
}三角形类
@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
<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原型。我不知道为什么上面的代码不能正常工作。我做了很多研究,但没有找到任何答案
System.out.println(t == t1); this gives me true while ideally it should be false.发布于 2018-06-10 15:49:08
由于ApplicationContext有几个实现,所以XML配置使用了XML。
通过XML方式,您只需在bean元素中设置作用域属性。
AnnotationConfigApplicationContext是ApplicationContext的实现,用于基于Java的配置手段,用于基于注释的配置,如@Bean等。
如果您想使用注释方式,那么您需要定义基于Java的配置,并且需要使用AnnotationConfigApplicationContext对象。
@Configuration
public class AppConfiguration{
@Bean
@Scope("prototype")
public Triangle triangle(){
return new Triangle();
}
}然后得到豆子如下所示。
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfiguration.class);
Triangle triangle= ctx.getBean(Triangle.class);发布于 2018-06-11 08:24:09
将Triangle声明为prototype没有效果,因为它在spring.xml中实例化为单例。
关键是每次您需要一个新的原型bean时都要一个新的原型bean。
这可以通过使用配置类(并从<bean id="triangle" ...中删除spring.xml )来实现。
package com.spring.demo;
@Configuration
public class MyProvider {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Triangle create() {
return new Triangle();
}
}用法如下:
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。
发布于 2018-06-13 15:54:59
对于注释配置,我们使用AnnotationConfigApplicationContext --抽象 Application Context的实现
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);
}
}教师班
public class Teacher {
public void print(String msg){
System.out.println("TEACHER -->"+msg);
}TeacherConfig
@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);的输出为真
https://stackoverflow.com/questions/50784972
复制相似问题