構造注入
如何給構造方法中的參數注入方法呢如下
首先bean代碼如下
package cn.pojo; public class Greeting { /** * 說的話 */ private String words; /** * 說話的人 */ private String person;
private Greeting greeting; public void sayGreeting() { System.out.println(person+"說:"+words); } public Greeting getGreeting() { return greeting; } public void setGreeting(Greeting greeting) { this.greeting = greeting; } public Greeting() { // TODO Auto-generated constructor stub } public Greeting(String words1,String person1) { this.words=words1; this.person=person1; } public String getWords() { return words; } public void setWords(String words) { this.words = words; } public String getPerson() { return person; } public void setPerson(String person) { this.person = person; } }
xml注入代碼如下
<!-- id為實例后對象名 class為對象的類型 --> <bean id="greeting1" class="cn.pojo.Greeting"> <!--name為構造參數中的參數名稱 value為值 --> <constructor-arg value="三天不打鬼子,手都不自在" name="words1"/> <constructor-arg value="小兵張嘎" name="person1"/> </bean>
這段個代碼跟下面這段Java代碼的意思是一樣的,實例化一個名叫 greeting1的對象並且傳入兩個參數 ,name則對應參數的名稱
Greeting greeting1 = new Greeting("三天不打鬼子,手都不自在","小兵張嘎");
測試代碼如下
//找到配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Greeting greeting1 = (Greeting)context.getBean("greeting1"); greeting1.sayGreeting();
結果如下
注入屬性
注入代碼如下
<bean id="greeting1" class="cn.pojo.Greeting"> <!--name為構造參數中的參數名稱 value為值 --> <constructor-arg value="三天不打鬼子,手都不自在" name="words1"/> <constructor-arg value="小兵張嘎" name="person1"/> <!-- 此段代碼相當於 實例化Greeting類后調用set方法對屬性名為greeting的屬性進行賦值,只不過賦值的類型是自定義的類類型--> <property name="greeting" ref="greeting2"/> </bean> <bean id="greeting2" class="cn.pojo.Greeting"> <!--name為構造參數中的參數名稱 value為值 給Greeting中的words和person賦值,相當於調用set方法進行賦值--> <property name="words" value="世界上有十種人,認識二進制和不認識二進制的"/> <property name="person" value="Rod"/> </bean>
再編寫bean時需要注意些setget方法,否則注入時將會出現異常
測試類代碼
//找到配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Greeting greeting1 = (Greeting)context.getBean("greeting1"); greeting1.getGreeting().sayGreeting(); greeting1.sayGreeting();
運行結果
p命名空間注入
首先使用p需要導入 xmlns:p="http://www.springframework.org/schema/p" 才能使用 這個地址訪問會出404不用管 是正常的可以用。注意區分大小寫
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
p命名空間簡化了屬性的注入,簡化了配置的方法,大大的簡化了配置的工作量 如下
<!-- 對類類型的屬性進行賦值分號后為 屬性名加-ref 即 greeting-ref--> <bean id="greeting1" class="cn.pojo.Greeting" p:greeting-ref="greeting2"> <!--name為構造參數中的參數名稱 value為值 --> <constructor-arg value="三天不打鬼子,手都不自在" name="words1"/> <constructor-arg value="小兵張嘎" name="person1"/> </bean> <!-- 分號后面為 屬性名稱 --> <bean id="greeting2" class="cn.pojo.Greeting" p:words="世界上有十種人,認識二進制和不認識二進制的" p:person="Rod"/>
使用注解定義bean
注意:如果是jdk1.8的最好不要用spring3,或者使用jdk1.7。否則很可能會出現不兼容,之前我就親身經歷了這個問題。於是我就換成了spring 4
jar下載網址:http://repo.spring.io/release/org/springframework/spring/
使用注解定義bean又進一步的減少了配置文件的代碼量
首先定義一個接口然后仔定義實現類
//通過注解定義了一個dao,此注解相當於 實例化了一個名叫 userDao UserDaoImppl類型的對象,是跟在xml中定義了一個bean一樣的效果 @Component("userDao") public class UserDaoImpl implements UserDao { @Override public void save(User u) { System.out.println("保存用戶信息到數據庫!"); } }
編寫業務層
//此注解相當於實例化了名為 userService的對象,也是跟xml定義了一個bean一樣的效果 @Service("userService") public class UserServiceImpl implements UserService{ //查找名為userDao的bean,並注入給dao屬性 @Resource(name="userDao") private UserDao dao; public UserDao getDao() { return dao; } public void setDao(UserDao userDao) { this.dao = userDao; } @Override public void save(User u) { // TODO Auto-generated method stub dao.save(u); } }
或者這樣也是可以的
//此注解相當於實例化了名為 userService的對象 @Service("userService") public class UserServiceImpl implements UserService{ //查找名為dao的bean,並注入給dao屬性 @Autowired @Qualifier("userDao") private UserDao dao; public UserDao getDao() { return dao; } public void setDao(UserDao userDao) { this.dao = userDao; } }
或者這樣
@Autowired private UserDao dao;
亦或者這樣
@Resource private UserDao dao;
上述的幾種方式結果都是一樣的 @Resource和@Autowired默認會尋找符合條件的bean 而 @Component("userDao")正好滿足。而他們的賦值是跟setget沒有關系的即使去掉也
沒得事
userConfig代碼如下
<?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" 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 "> <!-- 掃描指定目錄下的所有文件 可以直接寫 所有包的“父親”cn也可以 --> <context:component-scan base-package="cn.dao.impl,cn.service.impl"/> </beans>
測試類代碼如下
ApplicationContext context = new ClassPathXmlApplicationContext("userConfig.xml"); UserService userDao = (UserServiceImpl)context.getBean("userService"); User u = new User(); userDao.save(u);
結果如下
- @Component 是一個泛化的概念,僅僅表示一個組件 (Bean) ,可以作用在任何層次。
- @Repository:用於標注Dao類
- @Service:用於標注業務類
- @Controller:用於標注控制權類
它們的作用都是定義一個bean,看到不同的注解的時候可以方便區分此bean時干嘛用的。