方式一:基于XML
<bean id="service" class="com.luo.service.ServiceImpl"></bean>
方式二:基于@Configuration、@Bean
@Configuration
public class AppConfig {
@Bean("student")
public Student student(){
Student student = new Student();
student.setAge(23);
student.setName("luo");
return student;
}
}
@Test
public void test03(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring3.xml");
Object appConfig = applicationContext.getBean("appConfig");
System.out.println(appConfig);
Object student = applicationContext.getBean("student");
System.out.println(student);
}
输出
"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=59883:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA\IntelliJ IDEA bin\rep\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\apache-maven-3.5.4-bin\rep\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;D:\apache-maven-3.5.4-bin\rep\commons-logging\commons-logging\1.2\commons-logging-1.2.jar"
config.AppConfig$$EnhancerBySpringCGLIB$$d7523392@1d9b7cce
Student{age=23, name='luo'}
Process finished with exit code 0
方式三:AnnotationConfigApplicationContext 注册配置类(一)
@Test
public void test02(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Object appConfig = ctx.getBean("appConfig");
System.out.println(appConfig);
Student stu = (Student) ctx.getBean("student");
System.out.println(stu);
ctx.close();
}
输出
"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=59883:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA\IntelliJ IDEA bin\rep\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\apache-maven-3.5.4-bin\rep\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;D:\apache-maven-3.5.4-bin\rep\commons-logging\commons-logging\1.2\commons-logging-1.2.jar"
config.AppConfig$$EnhancerBySpringCGLIB$$d7523392@1d9b7cce
Student{age=23, name='luo'}
Process finished with exit code 0
方式四:注册配置类(二)
@Test
public void test02(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Book.class);
ctx.refresh();//必须刷新,触发容器解析注解
Book book = (Book) ctx.getBean("book");
System.out.println(book);
ctx.close();
}
public class Book {}
输出
"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=59883:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA\IntelliJ IDEA bin\rep\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\apache-maven-3.5.4-bin\rep\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;D:\apache-maven-3.5.4-bin\rep\commons-logging\commons-logging\1.2\commons-logging-1.2.jar"
config.Book@77846d2c
Process finished with exit code 0