使用Spring创建bean,并且为bean注入各种类型的参数值。
使用Spring为一个bean注入以下类型的参数值:
步骤一:创建项目,导入jar包
创建项目Spring02,导入Spring开发包,如下图:
图-1
步骤二: 创建bean
将Spring01项目中的Computer类复制到当前项目下,代码如下:
package com.tarena.bean; import java.io.Serializable; public class Computer implements Serializable { private String mainboard; // 主板 private String hdd; // 硬盘 private String ram; // 内存 public String getMainboard() { return mainboard; } public void setMainboard(String mainboard) { this.mainboard = mainboard; } public String getHdd() { return hdd; } public void setHdd(String hdd) { this.hdd = hdd; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } }
创建一个消息类MessageBean,代码如下:
package com.tarena.bean; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class MessageBean implements Serializable { // 基本值 private String name; private int age; // bean对象 private Computer computer; //集合 private List<String> langs; private Set<String> cities; private Map<String, Object> score; private Properties props; public void execute() { System.out.println("基本值:"); System.out.println(name); System.out.println(age); System.out.println("bean对象:"); if(computer != null) { System.out.println(computer.getMainboard()); System.out.println(computer.getHdd()); System.out.println(computer.getRam()); } System.out.println("编程语言:"); if(langs != null) { for(String lang : langs) { System.out.println(lang); } } System.out.println("城市:"); if(cities != null) { for(String city : cities) { System.out.println(city); } } System.out.println("分数:"); if(score != null) { Set<String> set = score.keySet(); for(String key : set) { System.out.println( key + " : " + score.get(key)); } } System.out.println("参数:"); if(props != null) { Set<Object> propKeySet = props.keySet(); for(Object key : propKeySet) { System.out.println( key + " : " + props.getProperty(key.toString())); } } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; } public List<String> getLangs() { return langs; } public void setLangs(List<String> langs) { this.langs = langs; } public Set<String> getCities() { return cities; } public void setCities(Set<String> cities) { this.cities = cities; } public Map<String, Object> getScore() { return score; } public void setScore(Map<String, Object> score) { this.score = score; } public Properties getProps() { return props; } public void setProps(Properties props) { this.props = props; } }
步骤三:声明bean
导入applicationContext.xml,在该配置文件中声明这2个bean,并将Computer注入给MessageBean,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="computer" class="com.tarena.bean.Computer"> <property name="mainboard" value="技嘉"/> <property name="hdd" value="希捷"/> <property name="ram" value="金士顿"/> </bean> <!-- 注入参数值 --> <bean id="msg" class="com.tarena.bean.MessageBean"> <property name="name"> <value>张三</value> </property> <property name="age" value="25"/> <property name="computer" ref="computer"/> <property name="langs"> <list> <value>Java</value> <value>php</value> <value>.net</value> </list> </property> <property name="cities"> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </property> <property name="score"> <map> <entry key="JSD1412001" value="78"/> <entry key="JSD1412002" value="68"/> <entry key="JSD1412003" value="94"/> </map> </property> <property name="props"> <props> <prop key="user">lhh</prop> <prop key="password">123456</prop> </props> </property> </bean> </beans>
步骤四:写测试代码
创建TestCase测试类,增加测试方法test1,代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.MessageBean; public class TestCase { /** * 注入参数值 */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); MessageBean bean = ctx.getBean("msg", MessageBean.class); bean.execute(); } }
步骤五:执行测试
执行测试方法test1,效果如下图:
图-2
Computer类完整代码如下:
package com.tarena.bean; import java.io.Serializable; public class Computer implements Serializable { private String mainboard; // 主板 private String hdd; // 硬盘 private String ram; // 内存 public String getMainboard() { return mainboard; } public void setMainboard(String mainboard) { this.mainboard = mainboard; } public String getHdd() { return hdd; } public void setHdd(String hdd) { this.hdd = hdd; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } }
MessageBean类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class MessageBean implements Serializable { // 基本值 private String name; private int age; // bean对象 private Computer computer; //集合 private List<String> langs; private Set<String> cities; private Map<String, Object> score; private Properties props; public void execute() { System.out.println("基本值:"); System.out.println(name); System.out.println(age); System.out.println("bean对象:"); if(computer != null) { System.out.println(computer.getMainboard()); System.out.println(computer.getHdd()); System.out.println(computer.getRam()); } System.out.println("编程语言:"); if(langs != null) { for(String lang : langs) { System.out.println(lang); } } System.out.println("城市:"); if(cities != null) { for(String city : cities) { System.out.println(city); } } System.out.println("分数:"); if(score != null) { Set<String> set = score.keySet(); for(String key : set) { System.out.println( key + " : " + score.get(key)); } } System.out.println("参数:"); if(props != null) { Set<Object> propKeySet = props.keySet(); for(Object key : propKeySet) { System.out.println( key + " : " + props.getProperty(key.toString())); } } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; } public List<String> getLangs() { return langs; } public void setLangs(List<String> langs) { this.langs = langs; } public Set<String> getCities() { return cities; } public void setCities(Set<String> cities) { this.cities = cities; } public Map<String, Object> getScore() { return score; } public void setScore(Map<String, Object> score) { this.score = score; } public Properties getProps() { return props; } public void setProps(Properties props) { this.props = props; } }
applicationContext.xml完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="computer" class="com.tarena.bean.Computer"> <property name="mainboard" value="技嘉"/> <property name="hdd" value="希捷"/> <property name="ram" value="金士顿"/> </bean> <!-- 注入参数值 --> <bean id="msg" class="com.tarena.bean.MessageBean"> <property name="name"> <value>张三</value> </property> <property name="age" value="25"/> <property name="computer" ref="computer"/> <property name="langs"> <list> <value>Java</value> <value>php</value> <value>.net</value> </list> </property> <property name="cities"> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </property> <property name="score"> <map> <entry key="JSD1412001" value="78"/> <entry key="JSD1412002" value="68"/> <entry key="JSD1412003" value="94"/> </map> </property> <property name="props"> <props> <prop key="user">lhh</prop> <prop key="password">123456</prop> </props> </property> </bean> </beans>
TestCase类完整代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.MessageBean; public class TestCase { /** * 注入参数值 */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); MessageBean bean = ctx.getBean("msg", MessageBean.class); bean.execute(); } }
先声明集合bean,然后采用引用的方式将这些bean注入给MessageBean。
使用<util>标签声明集合bean,然后在MessageBean下使用<property>标签引用这些集合bean。
步骤一:声明bean
在applicationContext.xml中声明集合bean,然后新声明一个MessageBean类型的bean,让这个bean引用集合bean,追加代码如下:
<!-- 声明集合bean --> <util:list id="langList"> <value>c++</value> <value>python</value> </util:list> <util:set id="citySet"> <value>重庆</value> <value>天津</value> </util:set> <util:map id="scoreMap"> <entry key="JSD1412004" value="90"/> <entry key="JSD1412005" value="85"/> </util:map> <util:properties id="paramProp"> <prop key="user">tarena</prop> <prop key="password">123456</prop> </util:properties> <!-- 采用引用的方式注入集合 --> <bean id="msg2" class="com.tarena.bean.MessageBean"> <property name="langs" ref="langList"/> <property name="cities" ref="citySet"/> <property name="score" ref="scoreMap"/> <property name="props" ref="paramProp"/> </bean>
步骤二: 写测试代码
在TestCase中增加测试方法test2,追加代码如下:
/** * 采用引用的方式注入集合 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); MessageBean bean = ctx.getBean("msg2", MessageBean.class); bean.execute(); }
步骤三:执行测试
执行test2方法,效果如下图:
图-3
applicationContext.xml完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="computer" class="com.tarena.bean.Computer"> <property name="mainboard" value="技嘉"/> <property name="hdd" value="希捷"/> <property name="ram" value="金士顿"/> </bean> <!-- 注入参数值 --> <bean id="msg" class="com.tarena.bean.MessageBean"> <property name="name"> <value>张三</value> </property> <property name="age" value="25"/> <property name="computer" ref="computer"/> <property name="langs"> <list> <value>Java</value> <value>php</value> <value>.net</value> </list> </property> <property name="cities"> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </property> <property name="score"> <map> <entry key="JSD1412001" value="78"/> <entry key="JSD1412002" value="68"/> <entry key="JSD1412003" value="94"/> </map> </property> <property name="props"> <props> <prop key="user">lhh</prop> <prop key="password">123456</prop> </props> </property> </bean> <!-- 声明集合bean --> <util:list id="langList"> <value>c++</value> <value>python</value> </util:list> <util:set id="citySet"> <value>重庆</value> <value>天津</value> </util:set> <util:map id="scoreMap"> <entry key="JSD1412004" value="90"/> <entry key="JSD1412005" value="85"/> </util:map> <util:properties id="paramProp"> <prop key="user">tarena</prop> <prop key="password">123456</prop> </util:properties> <!-- 采用引用的方式注入集合 --> <bean id="msg2" class="com.tarena.bean.MessageBean"> <property name="langs" ref="langList"/> <property name="cities" ref="citySet"/> <property name="score" ref="scoreMap"/> <property name="props" ref="paramProp"/> </bean> </beans>
TestCase类完整代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.MessageBean; public class TestCase { /** * 注入参数值 */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); MessageBean bean = ctx.getBean("msg", MessageBean.class); bean.execute(); } /** * 采用引用的方式注入集合 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); MessageBean bean = ctx.getBean("msg2", MessageBean.class); bean.execute(); } }
通过表达式的方式,给bean注入值。
新建一个bean,通过表达式的方式引用MessageBean等bean的数据,注入给这个新建的bean。
步骤一:创建bean
创建DemoBean,代码如下:
package com.tarena.bean; import java.io.Serializable; public class DemoBean implements Serializable { private String name; private String lang; private String score; private int pageSize; public void execute() { System.out.println("name:" + name); System.out.println("lang:" + lang); System.out.println("score:" + score); System.out.println("pageSize:" + pageSize); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
步骤二: 创建资源文件
创建一个资源文件const.properties,用来封装系统常量,代码如下:
#max rows in one page PAGE_SIZE=10
步骤三:声明bean
在applicationContext.xml中,通过<util:properties>标签声明一个id=”const”的bean,加载const.properties中的信息。然后再声明DemoBean,使用表达式读取MessageBean和id=”const”的bean,将值注入给DemoBean,追加代码如下:
<!-- 声明Properties集合,读取const.properties参数 --> <util:properties id="const" location="classpath:const.properties"/> <!-- 注入表达式 --> <bean id="demo" class="com.tarena.bean.DemoBean"> <property name="name" value="#{msg.name}"/> <property name="lang" value="#{msg.langs[0]}"/> <property name="score" value="#{msg.score.JSD1412001}"/> <property name="pageSize" value="#{const.PAGE_SIZE}"/> </bean>
步骤四:写测试代码
在TestCase中增加测试方法test3,追加代码如下:
/** * 注入表达式 */ @Test public void test3() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); DemoBean bean = ctx.getBean("demo", DemoBean.class); bean.execute(); }
步骤五:执行测试
执行test3方法,效果如下图:
图-4
DemoBean完整代码如下:
package com.tarena.bean; import java.io.Serializable; public class DemoBean implements Serializable { private String name; private String lang; private String score; private int pageSize; public void execute() { System.out.println("name:" + name); System.out.println("lang:" + lang); System.out.println("score:" + score); System.out.println("pageSize:" + pageSize); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
const.properties完整代码如下:
#max rows in one page PAGE_SIZE=10
applicationContext.xml完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="computer" class="com.tarena.bean.Computer"> <property name="mainboard" value="技嘉"/> <property name="hdd" value="希捷"/> <property name="ram" value="金士顿"/> </bean> <!-- 注入参数值 --> <bean id="msg" class="com.tarena.bean.MessageBean"> <property name="name"> <value>张三</value> </property> <property name="age" value="25"/> <property name="computer" ref="computer"/> <property name="langs"> <list> <value>Java</value> <value>php</value> <value>.net</value> </list> </property> <property name="cities"> <set> <value>北京</value> <value>上海</value> <value>广州</value> </set> </property> <property name="score"> <map> <entry key="JSD1412001" value="78"/> <entry key="JSD1412002" value="68"/> <entry key="JSD1412003" value="94"/> </map> </property> <property name="props"> <props> <prop key="user">lhh</prop> <prop key="password">123456</prop> </props> </property> </bean> <!-- 声明集合bean --> <util:list id="langList"> <value>c++</value> <value>python</value> </util:list> <util:set id="citySet"> <value>重庆</value> <value>天津</value> </util:set> <util:map id="scoreMap"> <entry key="JSD1412004" value="90"/> <entry key="JSD1412005" value="85"/> </util:map> <util:properties id="paramProp"> <prop key="user">tarena</prop> <prop key="password">123456</prop> </util:properties> <!-- 采用引用的方式注入集合 --> <bean id="msg2" class="com.tarena.bean.MessageBean"> <property name="langs" ref="langList"/> <property name="cities" ref="citySet"/> <property name="score" ref="scoreMap"/> <property name="props" ref="paramProp"/> </bean> <!-- 声明Properties集合,读取const.properties参数 --> <util:properties id="const" location="classpath:const.properties"/> <!-- 注入表达式 --> <bean id="demo" class="com.tarena.bean.DemoBean"> <property name="name" value="#{msg.name}"/> <property name="lang" value="#{msg.langs[0]}"/> <property name="score" value="#{msg.score.JSD1412001}"/> <property name="pageSize" value="#{const.PAGE_SIZE}"/> </bean> </beans>
使用Spring注解代替XML配置来声明bean,并使用注解管理bean的作用域和生命周期。
步骤一:创建项目,导入jar包
创建项目Spring02-2,导入Spring开发包,如下图:
图-5
步骤二: 导入配置文件
导入Spring配置文件applicationContext.xml,开启注解扫描,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.tarena"/> </beans>
步骤三:创建并声明bean
创建ExampleBean,并使用@Component注解声明这个bean,代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("example") public class ExampleBean implements Serializable { public ExampleBean() { System.out.println("实例化ExampleBean:" + this); } public void init() { System.out.println("初始化ExampleBean"); } public void destroy() { System.out.println("销毁ExampleBean"); } public void execute() { System.out.println("执行execute方法"); } }
步骤四:测试使用注解声明bean
创建测试类TestCase,增加测试方法test1,代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.ExampleBean; import com.tarena.bean.Manager; import com.tarena.bean.Programmer; import com.tarena.bean.Salesman; import com.tarena.bean.Student; import com.tarena.bean.Teacher; public class TestCase { /** * 使用注解声明bean */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); System.out.println(bean); } }
执行test1方法,效果如下图:
图-6
步骤五:测试bean的作用域
在TestCase中增加test2方法,追加代码如下:
/** * 使用注解声明bean的作用域 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean1 = ctx.getBean("example", ExampleBean.class); ExampleBean bean2 = ctx.getBean("example", ExampleBean.class); System.out.println(bean1==bean2); }
执行该方法,效果如下图:
图-7
在ExampleBean类上增加@scope(“prototype”),追加代码如下:
@Component("example") @Scope("prototype") public class ExampleBean implements Serializable {
再次执行test2方法,效果如下图:
图-8
步骤六:测试bean的生命周期
在ExampleBean上使用注解声明其初始化方法和销毁方法,并将该bean改为单例的,代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("example") @Scope("singleton") public class ExampleBean implements Serializable { public ExampleBean() { System.out.println("实例化ExampleBean:" + this); } @PostConstruct public void init() { System.out.println("初始化ExampleBean"); } @PreDestroy public void destroy() { System.out.println("销毁ExampleBean"); } public void execute() { System.out.println("执行execute方法"); } }
在TestCase中增加测试方法test3,追加代码如下:
/** * 使用注解声明bean的生命周期 */ @Test public void test3() { String cfg = "applicationContext.xml"; AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); bean.execute(); ctx.close(); }
执行该测试方法,效果如下图:
图-9
applicationContext.xml完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.tarena"/> </beans>
ExampleBean完整代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("example") @Scope("singleton") public class ExampleBean implements Serializable { public ExampleBean() { System.out.println("实例化ExampleBean:" + this); } @PostConstruct public void init() { System.out.println("初始化ExampleBean"); } @PreDestroy public void destroy() { System.out.println("销毁ExampleBean"); } public void execute() { System.out.println("执行execute方法"); } }
TestCase完整代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.ExampleBean; import com.tarena.bean.Manager; import com.tarena.bean.Programmer; import com.tarena.bean.Salesman; import com.tarena.bean.Student; import com.tarena.bean.Teacher; public class TestCase { /** * 使用注解声明bean */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); System.out.println(bean); } /** * 使用注解声明bean的作用域 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean1 = ctx.getBean("example", ExampleBean.class); ExampleBean bean2 = ctx.getBean("example", ExampleBean.class); System.out.println(bean1==bean2); } /** * 使用注解声明bean的生命周期 */ @Test public void test3() { String cfg = "applicationContext.xml"; AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); bean.execute(); ctx.close(); } }
使用@Autowired注解注入依赖的bean。
步骤一:创建并声明bean
创建Computer类,并使用@Component注解将其声明到容器中,代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.stereotype.Component; @Component public class Computer implements Serializable { private String mainboard; // 主板 private String hdd; // 硬盘 private String ram; // 内存 public String getMainboard() { return mainboard; } public void setMainboard(String mainboard) { this.mainboard = mainboard; } public String getHdd() { return hdd; } public void setHdd(String hdd) { this.hdd = hdd; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } }
创建Programmer类,并使用@Component注解将其声明到容器中,然后使用@Autowired注解在构造器上将Computer注入进来,代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Programmer implements Serializable { private Computer computer; @Autowired public Programmer( @Qualifier("computer") Computer computer) { this.computer = computer; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; } }
步骤二: 测试以构造方式注入bean
在TestCase中增加测试方法test4,追加代码如下:
/** * 使用@Autowired,以构造方式注入bean */ @Test public void test4() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Programmer prog = ctx.getBean("programmer", Programmer.class); System.out.println(prog); System.out.println(prog.getComputer()); }
执行该测试方法,效果如下图:
图-10
步骤三:创建并声明bean
创建Teacher类,并使用注解将其声明到容器中,然后使用@Autowired注解在set方法上将Computer注入,代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Teacher implements Serializable { private Computer computer; @Autowired public void setComputer( @Qualifier("computer") Computer computer) { this.computer = computer; System.out.println("Teacher"); } public Computer getComputer() { return computer; } }
步骤四:测试在setter上加注解的方式注入bean
在TestCase里增加测试方法test5,追加代码如下:
/** * 在setter上使用@Autowired,以setter方式注入bean */ @Test public void test5() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Teacher teacher = ctx.getBean("teacher", Teacher.class); System.out.println(teacher); System.out.println(teacher.getComputer()); }
执行该测试方法,效果如下图:
图-11
步骤五:创建并声明bean
创建Student类,并使用注解将其声明到容器中,然后在属性上加注解将Computer注入,代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Student implements Serializable { @Autowired @Qualifier("computer") private Computer computer; public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; System.out.println("Student"); } }
步骤六:测试在属性上加注解的方式注入bean
在TestCase中增加测试方法test6,追加代码如下:
/** * 在属性上使用@Autowired,以setter方式注入bean */ @Test public void test6() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Student student = ctx.getBean("student", Student.class); System.out.println(student); System.out.println(student.getComputer()); }
执行该测试方法,效果如下图:
图-12
Computer类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.stereotype.Component; @Component public class Computer implements Serializable { private String mainboard; // 主板 private String hdd; // 硬盘 private String ram; // 内存 public String getMainboard() { return mainboard; } public void setMainboard(String mainboard) { this.mainboard = mainboard; } public String getHdd() { return hdd; } public void setHdd(String hdd) { this.hdd = hdd; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } }
Programmer类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Programmer implements Serializable { private Computer computer; @Autowired public Programmer( @Qualifier("computer") Computer computer) { this.computer = computer; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; } }
Teacher类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Teacher implements Serializable { private Computer computer; @Autowired public void setComputer( @Qualifier("computer") Computer computer) { this.computer = computer; System.out.println("Teacher"); } public Computer getComputer() { return computer; } }
Student类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Student implements Serializable { @Autowired @Qualifier("computer") private Computer computer; public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; System.out.println("Student"); } }
TestCase类完整代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.ExampleBean; import com.tarena.bean.Manager; import com.tarena.bean.Programmer; import com.tarena.bean.Salesman; import com.tarena.bean.Student; import com.tarena.bean.Teacher; public class TestCase { /** * 使用注解声明bean */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); System.out.println(bean); } /** * 使用注解声明bean的作用域 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean1 = ctx.getBean("example", ExampleBean.class); ExampleBean bean2 = ctx.getBean("example", ExampleBean.class); System.out.println(bean1==bean2); } /** * 使用注解声明bean的生命周期 */ @Test public void test3() { String cfg = "applicationContext.xml"; AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); bean.execute(); ctx.close(); } /** * 使用@Autowired,以构造方式注入bean */ @Test public void test4() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Programmer prog = ctx.getBean("programmer", Programmer.class); System.out.println(prog); System.out.println(prog.getComputer()); } /** * 在setter上使用@Autowired,以setter方式注入bean */ @Test public void test5() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Teacher teacher = ctx.getBean("teacher", Teacher.class); System.out.println(teacher); System.out.println(teacher.getComputer()); } /** * 在属性上使用@Autowired,以setter方式注入bean */ @Test public void test6() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Student student = ctx.getBean("student", Student.class); System.out.println(student); System.out.println(student.getComputer()); } }
使用@Resource注解注入依赖的bean。
步骤一:创建并声明bean
创建Manager类,代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component public class Manager implements Serializable { private Computer computer; @Resource(name="computer") public void setComputer(Computer computer) { this.computer = computer; System.out.println("Manager"); } public Computer getComputer() { return computer; } }
步骤二: 测试在setter上加注解的方式注入bean
在TestCase中增加测试方法test7,追加代码如下:
/** * 在setter上使用@Resource,以setter方式注入bean */ @Test public void test7() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Manager manager = ctx.getBean("manager", Manager.class); System.out.println(manager); System.out.println(manager.getComputer()); }
执行该测试方法,效果如下图:
图-13
步骤三:创建并声明bean
创建Salesman类,代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component public class Salesman implements Serializable { @Resource private Computer computer; public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; System.out.println("Salesman"); } }
步骤四:测试在属性上加注解的方式注入bean
在TestCase中增加测试方法test8,追加代码如下:
/** * 在属性上使用@Resource,以setter方式注入bean */ @Test public void test8() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Salesman man = ctx.getBean("salesman", Salesman.class); System.out.println(man); System.out.println(man.getComputer()); }
执行该测试方法,效果如下图:
图-14
Manager类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component public class Manager implements Serializable { private Computer computer; @Resource(name="computer") public void setComputer(Computer computer) { this.computer = computer; System.out.println("Manager"); } public Computer getComputer() { return computer; } }
Salesman类完整代码如下:
package com.tarena.bean; import java.io.Serializable; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component public class Salesman implements Serializable { @Resource private Computer computer; public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; System.out.println("Salesman"); } }
TestCase类完整代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.ExampleBean; import com.tarena.bean.Manager; import com.tarena.bean.Programmer; import com.tarena.bean.Salesman; import com.tarena.bean.Student; import com.tarena.bean.Teacher; public class TestCase { /** * 使用注解声明bean */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); System.out.println(bean); } /** * 使用注解声明bean的作用域 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean1 = ctx.getBean("example", ExampleBean.class); ExampleBean bean2 = ctx.getBean("example", ExampleBean.class); System.out.println(bean1==bean2); } /** * 使用注解声明bean的生命周期 */ @Test public void test3() { String cfg = "applicationContext.xml"; AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); bean.execute(); ctx.close(); } /** * 使用@Autowired,以构造方式注入bean */ @Test public void test4() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Programmer prog = ctx.getBean("programmer", Programmer.class); System.out.println(prog); System.out.println(prog.getComputer()); } /** * 在setter上使用@Autowired,以setter方式注入bean */ @Test public void test5() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Teacher teacher = ctx.getBean("teacher", Teacher.class); System.out.println(teacher); System.out.println(teacher.getComputer()); } /** * 在属性上使用@Autowired,以setter方式注入bean */ @Test public void test6() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Student student = ctx.getBean("student", Student.class); System.out.println(student); System.out.println(student.getComputer()); } /** * 在setter上使用@Resource,以setter方式注入bean */ @Test public void test7() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Manager manager = ctx.getBean("manager", Manager.class); System.out.println(manager); System.out.println(manager.getComputer()); } /** * 在属性上使用@Resource,以setter方式注入bean */ @Test public void test8() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Salesman man = ctx.getBean("salesman", Salesman.class); System.out.println(man); System.out.println(man.getComputer()); } }
使用注解,通过表达式给一个bean注入数据。
在@Value注解上写表达式,可以给bean注入数据。
步骤一:创建资源文件
创建资源文件const.properties,代码如下:
#max rows in one page PAGE_SIZE=10
步骤二: 读取资源文件信息
在applicationContext.xml中,使用<util>标签声明一个bean,读取资源文件的信息,追加代码如下:
<!-- 声明Properties集合,读取const.properties参数 --> <util:properties id="const" location="classpath:const.properties"/>
步骤三:将读取到的信息注入bean
创建DemoBean类,并使用注解将读取的资源文件数据注入到这个bean中,代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DemoBean implements Serializable { private String name; private String lang; private String score; @Value("#{const.PAGE_SIZE}") private int pageSize; public void execute() { System.out.println("name:" + name); System.out.println("lang:" + lang); System.out.println("score:" + score); System.out.println("pageSize:" + pageSize); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
步骤四:写测试代码
在TestCase类中增加测试方法test9,追加代码如下:
/** * 使用注解注入表达式 */ @Test public void test9() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); DemoBean demo = ctx.getBean("demoBean", DemoBean.class); demo.execute(); }
步骤五:执行测试
执行test9,效果如下图:
图-15
const.properties完整代码如下:
#max rows in one page PAGE_SIZE=10
applicationContext.xml完整代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 开启注解扫描 --> <context:component-scan base-package="com.tarena"/> <!-- 声明Properties集合,读取const.properties参数 --> <util:properties id="const" location="classpath:const.properties"/> </beans>
DemoBean完整代码如下:
package com.tarena.bean; import java.io.Serializable; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class DemoBean implements Serializable { private String name; private String lang; private String score; @Value("#{const.PAGE_SIZE}") private int pageSize; public void execute() { System.out.println("name:" + name); System.out.println("lang:" + lang); System.out.println("score:" + score); System.out.println("pageSize:" + pageSize); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
TestCase完整代码如下:
package com.tarena.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.bean.DemoBean; import com.tarena.bean.ExampleBean; import com.tarena.bean.Manager; import com.tarena.bean.Programmer; import com.tarena.bean.Salesman; import com.tarena.bean.Student; import com.tarena.bean.Teacher; public class TestCase { /** * 使用注解声明bean */ @Test public void test1() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); System.out.println(bean); } /** * 使用注解声明bean的作用域 */ @Test public void test2() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean1 = ctx.getBean("example", ExampleBean.class); ExampleBean bean2 = ctx.getBean("example", ExampleBean.class); System.out.println(bean1==bean2); } /** * 使用注解声明bean的生命周期 */ @Test public void test3() { String cfg = "applicationContext.xml"; AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); ExampleBean bean = ctx.getBean("example", ExampleBean.class); bean.execute(); ctx.close(); } /** * 使用@Autowired,以构造方式注入bean */ @Test public void test4() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Programmer prog = ctx.getBean("programmer", Programmer.class); System.out.println(prog); System.out.println(prog.getComputer()); } /** * 在setter上使用@Autowired,以setter方式注入bean */ @Test public void test5() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Teacher teacher = ctx.getBean("teacher", Teacher.class); System.out.println(teacher); System.out.println(teacher.getComputer()); } /** * 在属性上使用@Autowired,以setter方式注入bean */ @Test public void test6() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Student student = ctx.getBean("student", Student.class); System.out.println(student); System.out.println(student.getComputer()); } /** * 在setter上使用@Resource,以setter方式注入bean */ @Test public void test7() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Manager manager = ctx.getBean("manager", Manager.class); System.out.println(manager); System.out.println(manager.getComputer()); } /** * 在属性上使用@Resource,以setter方式注入bean */ @Test public void test8() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); Salesman man = ctx.getBean("salesman", Salesman.class); System.out.println(man); System.out.println(man.getComputer()); } /** * 使用注解注入表达式 */ @Test public void test9() { String cfg = "applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg); DemoBean demo = ctx.getBean("demoBean", DemoBean.class); demo.execute(); } }