spring框架中的对象创建策略是什么
我是spring框架的新手,只是在玩它。在这期间,我遇到了一个奇怪的输出。我试着调试。但是没有得到答案(尽管我没有深入研究框架代码) 我想做什么:我只想创建一个spring框架中的对象创建策略是什么,spring,Spring,我是spring框架的新手,只是在玩它。在这期间,我遇到了一个奇怪的输出。我试着调试。但是没有得到答案(尽管我没有深入研究框架代码) 我想做什么:我只想创建一个User类的实例。值{city='Sirsa2',i1=12} 我有以下四个代码文件: IntTest.java package Task4; import org.springframework.stereotype.Service; //Just creating an integer. Since Integer class i
User
类的实例。值{city='Sirsa2',i1=12}
我有以下四个代码文件:
IntTest.java
package Task4;
import org.springframework.stereotype.Service;
//Just creating an integer. Since Integer class is final so, I have to extend Number class. So I also needed to override abstract metods.
@Service
public class IntTest extends Number {
Integer i1;
public Integer getI1() {
return i1;
}
public void setI1(Integer i1) {
this.i1 = i1;
}
public IntTest() {
i1 = 90;
}
@Override
public double doubleValue() {
return i1.doubleValue();
}
@Override
public float floatValue() {
return i1.floatValue();
}
@Override
public int intValue() {
return i1.intValue();
}
@Override
public long longValue() {
return i1.longValue();
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class User {
Address address;
Integer i1;
User(Address address, IntTest t1)
{
this.address= address;
i1 = t1.getI1();
System.out.println("1. Point " + t1.getI1());
}
public void getInfo() {
System.out.println(address.getCity() + " | "+i1.intValue());
}
}
package Task4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("Task3")
public class Config {}
package Task4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String args[]) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(Config.class);
context.refresh();
Address address1=(Address)context.getBean(Address.class);
address1.setCity("Sirsa");
Address address2=(Address)context.getBean(Address.class);
address2.setCity("Sirsa2");
IntTest intTest=(IntTest)context.getBean(IntTest.class);
System.out.println("2. Point " + intTest.getI1());
intTest.setI1(12);
System.out.println("3. Point " + intTest.getI1());
User user=(User) context.getBean(User.class);
user.getInfo();
}
}
Address.java
package Task4;
import org.springframework.stereotype.Service;
//Just creating an integer. Since Integer class is final so, I have to extend Number class. So I also needed to override abstract metods.
@Service
public class IntTest extends Number {
Integer i1;
public Integer getI1() {
return i1;
}
public void setI1(Integer i1) {
this.i1 = i1;
}
public IntTest() {
i1 = 90;
}
@Override
public double doubleValue() {
return i1.doubleValue();
}
@Override
public float floatValue() {
return i1.floatValue();
}
@Override
public int intValue() {
return i1.intValue();
}
@Override
public long longValue() {
return i1.longValue();
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class User {
Address address;
Integer i1;
User(Address address, IntTest t1)
{
this.address= address;
i1 = t1.getI1();
System.out.println("1. Point " + t1.getI1());
}
public void getInfo() {
System.out.println(address.getCity() + " | "+i1.intValue());
}
}
package Task4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("Task3")
public class Config {}
package Task4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String args[]) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(Config.class);
context.refresh();
Address address1=(Address)context.getBean(Address.class);
address1.setCity("Sirsa");
Address address2=(Address)context.getBean(Address.class);
address2.setCity("Sirsa2");
IntTest intTest=(IntTest)context.getBean(IntTest.class);
System.out.println("2. Point " + intTest.getI1());
intTest.setI1(12);
System.out.println("3. Point " + intTest.getI1());
User user=(User) context.getBean(User.class);
user.getInfo();
}
}
User.java
package Task4;
import org.springframework.stereotype.Service;
//Just creating an integer. Since Integer class is final so, I have to extend Number class. So I also needed to override abstract metods.
@Service
public class IntTest extends Number {
Integer i1;
public Integer getI1() {
return i1;
}
public void setI1(Integer i1) {
this.i1 = i1;
}
public IntTest() {
i1 = 90;
}
@Override
public double doubleValue() {
return i1.doubleValue();
}
@Override
public float floatValue() {
return i1.floatValue();
}
@Override
public int intValue() {
return i1.intValue();
}
@Override
public long longValue() {
return i1.longValue();
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class User {
Address address;
Integer i1;
User(Address address, IntTest t1)
{
this.address= address;
i1 = t1.getI1();
System.out.println("1. Point " + t1.getI1());
}
public void getInfo() {
System.out.println(address.getCity() + " | "+i1.intValue());
}
}
package Task4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("Task3")
public class Config {}
package Task4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String args[]) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(Config.class);
context.refresh();
Address address1=(Address)context.getBean(Address.class);
address1.setCity("Sirsa");
Address address2=(Address)context.getBean(Address.class);
address2.setCity("Sirsa2");
IntTest intTest=(IntTest)context.getBean(IntTest.class);
System.out.println("2. Point " + intTest.getI1());
intTest.setI1(12);
System.out.println("3. Point " + intTest.getI1());
User user=(User) context.getBean(User.class);
user.getInfo();
}
}
Config.java
package Task4;
import org.springframework.stereotype.Service;
//Just creating an integer. Since Integer class is final so, I have to extend Number class. So I also needed to override abstract metods.
@Service
public class IntTest extends Number {
Integer i1;
public Integer getI1() {
return i1;
}
public void setI1(Integer i1) {
this.i1 = i1;
}
public IntTest() {
i1 = 90;
}
@Override
public double doubleValue() {
return i1.doubleValue();
}
@Override
public float floatValue() {
return i1.floatValue();
}
@Override
public int intValue() {
return i1.intValue();
}
@Override
public long longValue() {
return i1.longValue();
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class User {
Address address;
Integer i1;
User(Address address, IntTest t1)
{
this.address= address;
i1 = t1.getI1();
System.out.println("1. Point " + t1.getI1());
}
public void getInfo() {
System.out.println(address.getCity() + " | "+i1.intValue());
}
}
package Task4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("Task3")
public class Config {}
package Task4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String args[]) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(Config.class);
context.refresh();
Address address1=(Address)context.getBean(Address.class);
address1.setCity("Sirsa");
Address address2=(Address)context.getBean(Address.class);
address2.setCity("Sirsa2");
IntTest intTest=(IntTest)context.getBean(IntTest.class);
System.out.println("2. Point " + intTest.getI1());
intTest.setI1(12);
System.out.println("3. Point " + intTest.getI1());
User user=(User) context.getBean(User.class);
user.getInfo();
}
}
MainClass.java
package Task4;
import org.springframework.stereotype.Service;
//Just creating an integer. Since Integer class is final so, I have to extend Number class. So I also needed to override abstract metods.
@Service
public class IntTest extends Number {
Integer i1;
public Integer getI1() {
return i1;
}
public void setI1(Integer i1) {
this.i1 = i1;
}
public IntTest() {
i1 = 90;
}
@Override
public double doubleValue() {
return i1.doubleValue();
}
@Override
public float floatValue() {
return i1.floatValue();
}
@Override
public int intValue() {
return i1.intValue();
}
@Override
public long longValue() {
return i1.longValue();
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package Task4;
import org.springframework.stereotype.Service;
@Service
public class User {
Address address;
Integer i1;
User(Address address, IntTest t1)
{
this.address= address;
i1 = t1.getI1();
System.out.println("1. Point " + t1.getI1());
}
public void getInfo() {
System.out.println(address.getCity() + " | "+i1.intValue());
}
}
package Task4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("Task3")
public class Config {}
package Task4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainClass {
public static void main(String args[]) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(Config.class);
context.refresh();
Address address1=(Address)context.getBean(Address.class);
address1.setCity("Sirsa");
Address address2=(Address)context.getBean(Address.class);
address2.setCity("Sirsa2");
IntTest intTest=(IntTest)context.getBean(IntTest.class);
System.out.println("2. Point " + intTest.getI1());
intTest.setI1(12);
System.out.println("3. Point " + intTest.getI1());
User user=(User) context.getBean(User.class);
user.getInfo();
}
}
当我作为JavaApplication运行MainClass.java
时,我将获得以下输出:
1. Point 90
2. Point 90
3. Point 12
Sirsa2 | 90
但期望:
2. Point 90
3. Point 12
1. Point 12
Sirsa2 | 12
我不确定对象是如何创建的。但我认为:
- 要创建
类的对象,将调用构造函数User
User(地址,IntTest t1)
- 因为在构造函数中传递的
类的对象是Address
变量(因为它的值被打印为'Sirsa2',而不是'Sirsa')。所以我希望address1
类的对象应该是IntTest
变量李>i1
- 另外,我希望
类的构造函数应该在最后调用。但就产量而言,情况并非如此User
- 当我调试它时,我知道它正在创建第
context.refresh()行中的对象代码>在
中。但它是如何将城市的值设置为MainClass.java
,而不是将Sirsa2
的值设置为12的i1
我使用的是
Eclipse(Oxygen)
,java1.8.0\u121
,spring-framework-4.3.9。默认情况下,发布和commons-logging-1.2
,ApplicationContext实现急切地创建和配置所有单例bean,作为初始化过程的一部分。初始化过程在应用程序启动期间进行。之后,它将继续注入对象。默认情况下,对象是单例作用域,因此对整个应用程序都是相同的。是的,它将这样工作只有
因为System.out.println(“1.Point”+t1.getI1())代码>位于用户对象的构造函数中,同时启动应用程序spring尝试为此类用户创建对象,构造函数被调用,因此打印point1
如果你想要以下行为
2.第90点
3.第12点
1.第12点
Sirsa2 | 12
在您的bean上设置lazy init=“true”默认情况下,Spring bean是单例的,这意味着只创建了一个bean实例,可以在使用它的任何地方重用它。请参阅Spring框架文档中的。@Jesper感谢您提供此信息。但是为什么没有按照我的期望设定值呢。我知道我的例外很奇怪。但spring框架的行为也是如此;)(因为它选择的是Address
的值,而不是IntTest
)请注意,由于它们是单例,因此只有一个Address
对象,而address1
和address2
都指向同一个对象。您可以执行address1.setCity(“Sirsa”)代码>但行地址2.setCity(“Sirsa2”)代码>将覆盖它,因为它是同一个对象。同样,只有一个IntTest
对象。因为spring在spring容器中创建了所有对象。它们首先在应用程序启动时初始化。之后,它将继续注入对象。默认情况下,这些对象是单例作用域,因此对于整个应用程序来说都是相同的,但这并不重要。是的,您可以这样做吗是的,它只会像这样工作。