Spring不是丝豆
Spring not wire beans
我开始学习 spring 并想尝试一个例子。我写了 3 类 波纹管蝙蝠似乎我遗漏了一些东西,因为字符串没有按照我的意图去做。
package testPackage;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes=Config.class)
public class TestDrive {
@Autowired
private InterfaceClass obj;
public static void main(String[] args) {
TestDrive testDrive = new TestDrive();
System.out.println(testDrive.obj);
}
}
package testPackage;
import org.springframework.context.annotation.*;
public class TestClass implements InterfaceClass {
public String test = "test string";
@Bean
public TestClass getTestClass() {
TestClass testClass = new TestClass();
return testClass;
}
}
package testPackage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class Config {
}
当我 运行 来自 main 的这段代码时,我得到 "null" 打印。
为什么 Spring 不连接 "InterfaceClass obj"?
谢谢!
将 @Configuration
注释放在测试 class 之上:-
package testPackage;
import org.springframework.context.annotation.*;
@Configuration
public class TestClass implements InterfaceClass {
public String test = "test string";
@Bean
public TestClass getTestClass() {
TestClass testClass = new TestClass();
return testClass;
}
}
1) 在标记为 @Configuration 的 class 中声明您的 Spring Beans
2) 在main函数中,使用ApplicationContext获取TestDrive对象。 切勿将 Spring bean 对象与从 new 运算符创建的对象混合
试驾Class
public class TestDrive {
@Autowired
private InterfaceClass obj;
@Autowired
private ApplicationContext appContext;
public static void main(String[] args) {
//you will have to use this method, to get bean of TestDrive
// You should not use new operator, not advisable to mix new operator with spring beans
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
TestDrive drive = context.getBean(TestDrive.class);
System.out.println(drive.getString());
}
public String getString(){
String str = obj.getTestString();
return str;
}
}
测试Class
public class TestClass implements InterfaceClass{
public String test = "test string";
public String getTestString(){
return test;
}
}
界面Class
public interface InterfaceClass {
public String getTestString();
}
配置
@Configuration
public class Config {
@Bean
public InterfaceClass getTestClass() {
TestClass testClass = new TestClass();
return testClass;
}
@Bean
public TestDrive getTestDrive(){
return new TestDrive();
}
}
我开始学习 spring 并想尝试一个例子。我写了 3 类 波纹管蝙蝠似乎我遗漏了一些东西,因为字符串没有按照我的意图去做。
package testPackage;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes=Config.class)
public class TestDrive {
@Autowired
private InterfaceClass obj;
public static void main(String[] args) {
TestDrive testDrive = new TestDrive();
System.out.println(testDrive.obj);
}
}
package testPackage;
import org.springframework.context.annotation.*;
public class TestClass implements InterfaceClass {
public String test = "test string";
@Bean
public TestClass getTestClass() {
TestClass testClass = new TestClass();
return testClass;
}
}
package testPackage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class Config {
}
当我 运行 来自 main 的这段代码时,我得到 "null" 打印。 为什么 Spring 不连接 "InterfaceClass obj"?
谢谢!
将 @Configuration
注释放在测试 class 之上:-
package testPackage;
import org.springframework.context.annotation.*;
@Configuration
public class TestClass implements InterfaceClass {
public String test = "test string";
@Bean
public TestClass getTestClass() {
TestClass testClass = new TestClass();
return testClass;
}
}
1) 在标记为 @Configuration 的 class 中声明您的 Spring Beans
2) 在main函数中,使用ApplicationContext获取TestDrive对象。 切勿将 Spring bean 对象与从 new 运算符创建的对象混合
试驾Class
public class TestDrive {
@Autowired
private InterfaceClass obj;
@Autowired
private ApplicationContext appContext;
public static void main(String[] args) {
//you will have to use this method, to get bean of TestDrive
// You should not use new operator, not advisable to mix new operator with spring beans
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
TestDrive drive = context.getBean(TestDrive.class);
System.out.println(drive.getString());
}
public String getString(){
String str = obj.getTestString();
return str;
}
}
测试Class
public class TestClass implements InterfaceClass{
public String test = "test string";
public String getTestString(){
return test;
}
}
界面Class
public interface InterfaceClass {
public String getTestString();
}
配置
@Configuration
public class Config {
@Bean
public InterfaceClass getTestClass() {
TestClass testClass = new TestClass();
return testClass;
}
@Bean
public TestDrive getTestDrive(){
return new TestDrive();
}
}