創建 Maven 工程;
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
-
傳統 Spring 配置文件 的 HelloWorld
Person 類:
package com.kino.bean; /** * Created by RM on 2019/2/18. */ public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Person(String name, Integer age) { this.name = name; this.age = age; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
Spring 配置文件:
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.kino.bean.Person"> <property name="age" value="18"/> <property name="name" value="張三"/> </bean> </beans>
測試類:
package com.kino.test; import com.kino.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by RM on 2019/2/18. */ public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); } }
測試結果:
"D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\bin\java" "-javaagent:D:\SoftWare\IDEA\InstallationPath\IntelliJ IDEA 2017.2.2\lib\idea_rt.jar=55334:D:\SoftWare\IDEA\InstallationPath\IntelliJ IDEA 2017.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\charsets.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\deploy.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\access-bridge.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\cldrdata.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\dnsns.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\jaccess.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\jfxrt.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\localedata.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\nashorn.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunec.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunjce_provider.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunmscapi.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunpkcs11.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\zipfs.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\javaws.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jce.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jfr.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jfxswt.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jsse.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\management-agent.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\plugin.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\resources.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\rt.jar;D:\Work\IDEA\springannotation\target\classes;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-context\4.3.12.RELEASE\spring-context-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-aop\4.3.12.RELEASE\spring-aop-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-beans\4.3.12.RELEASE\spring-beans-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-core\4.3.12.RELEASE\spring-core-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-expression\4.3.12.RELEASE\spring-expression-4.3.12.RELEASE.jar" com.kino.test.MyTest 二月 18, 2019 10:20:38 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@156ce6a: startup date [Mon Feb 18 22:20:38 CST 2019]; root of context hierarchy 二月 18, 2019 10:20:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] Person{name='張三', age=18} Process finished with exit code 0
-
spring 註解配置
還是原來的 Person 類新增一個 配置類:
package com.kino.config; import com.kino.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by RM on 2019/2/18. */ // 配置類 == Spring 的配置文件 //@Configuration: 告訴 Spring, 這個類是一個配置類 @Configuration public class MyConfig { //給容器中註冊一個Bean; 類型爲返回值的類型; id默認爲用方法名作爲id @Bean public Person getPerson(){ return new Person("kino", 23); } }
測試類:
package com.kino.test; import com.kino.bean.Person; import com.kino.config.MyConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by RM on 2019/2/18. */ public class MyTest { public static void main(String[] args) { // ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); // Person person = (Person) context.getBean("person"); // System.out.println(person); ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); Person person = (Person) context.getBean("getPerson"); System.out.println(person); } }
測試結果:
"D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\bin\java" "-javaagent:D:\SoftWare\IDEA\InstallationPath\IntelliJ IDEA 2017.2.2\lib\idea_rt.jar=55635:D:\SoftWare\IDEA\InstallationPath\IntelliJ IDEA 2017.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\charsets.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\deploy.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\access-bridge.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\cldrdata.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\dnsns.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\jaccess.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\jfxrt.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\localedata.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\nashorn.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunec.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunjce_provider.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunmscapi.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\sunpkcs11.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\ext\zipfs.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\javaws.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jce.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jfr.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jfxswt.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\jsse.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\management-agent.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\plugin.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\resources.jar;D:\SoftWare\jdk\InstallationPath\jdk 1.8 161M\jre\lib\rt.jar;D:\Work\IDEA\springannotation\target\classes;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-context\4.3.12.RELEASE\spring-context-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-aop\4.3.12.RELEASE\spring-aop-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-beans\4.3.12.RELEASE\spring-beans-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-core\4.3.12.RELEASE\spring-core-4.3.12.RELEASE.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\SoftWare\maven\InstallationPath\apache-maven\lib\org\springframework\spring-expression\4.3.12.RELEASE\spring-expression-4.3.12.RELEASE.jar" com.kino.test.MyTest 二月 18, 2019 10:33:58 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@13c675d: startup date [Mon Feb 18 22:33:58 CST 2019]; root of context hierarchy Person{name='kino', age=23} Process finished with exit code 0
@Bean 註解:
//給容器中註冊一個Bean; 類型爲返回值的類型; id默認爲用方法名作爲id
@Bean
public Person getPerson(){
return new Person("kino", 23);
}
等同於配置文件的
<bean id="person" class="com.kino.bean.Person">
<property name="age" value="18"/>
<property name="name" value="張三"/>
</bean>
@Bean 註解源碼:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
//定義Id
/**
* @Bean(name="person1")
* public Person getPerson(){}
*
* Person person = (Person) context.getBean("person1");
*/
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
Autowire autowire() default Autowire.NO;
String initMethod() default "";
String destroyMethod() default "(inferred)";
}