前言
在看此文章之前建议先看通过xml配置文件的方式使用spring框架
MainConfiguration
@Configuration
public class MainConfiguration {
@Bean("student")
public Student student() {
return new Student();
};
}
此段代码与
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name = "student" class="com.test.bean.Student"/>
</beans>
作用相同
Main
public class Main
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class);
Student student = context.getBean("student", Student.class);
student.test();
}
}