spring的主要特點就是依賴注入和控制反轉,那麼注入的方式有多少種呢。答案是三種分別是註解,get和set方法,構造器注入,下面就來簡單的說下用法。
1.最簡單的是第二種,我們只需要在bean裏使用properties注入進去就可以了,如
- <bean id="personManager" class="org.lxh.impl.PersonManger">
- <property name="teacherDao" ref="teacherDao"></property>
- </bean>
這樣的話只需要在我們的bean里加入該dao的set方法就ok了,get方法要不要都可以
- private TeacherDao teacherDao;
- public TeacherDao getTeacherDao() {
- return teacherDao;
- }
- public void setTeacherDao(TeacherDao teacherDao) {
- this.teacherDao = teacherDao;
- }
2.構造器注入
這裏需要用到一個叫constructor-arg的標籤,裏面的屬性爲index(從0開始),ref,value(注入普通變量的時候用),type(注入普通變量的時候可以不寫),這裏的type表示的是接口
- <constructor-arg index="0" type="org.lxh.dao.StudentDao" ref="dao"></constructor-arg>
bean裏面這樣寫就OK 了,這裏要注意的是要留下默認的構造器
- private StudentDao dao;
- public PersonManger() {
- }
- public PersonManger(StudentDao dao) {
- this.dao = dao;
- }
3.使用註解注入
使用註解的時候,需要增加下面3句,並打開那個反射節點
- xmlns:context="http://www.springframework.org/schema/context"
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
配置文件大致如下
- <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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
使用註解有2種方式分別是:自動裝配,手工裝配,開發中建議使用手工裝配,這幾個註解分別爲@Resource(默認按名稱裝配),@Autowired(自動裝配默認是按類型裝配)
- // //註解注入的第一種方式
- // @Resource private StudentDao dao;
- //
- // public PersonManger() {
- //
- // }
- // public PersonManger(StudentDao dao) {
- // this.dao = dao;
- // }
- // 註解注入的第二種方式
- // private StudentDao dao;
- //
- // @Resource
- // public void setDao(StudentDao dao) {
- // this.dao = dao;
- // }
@Service,@Controller,@Repository,@Component,目前的話四個註解spring沒有做嚴格區分,隨便用哪一個都可以,主要用於組件自動掃描,要使用這種方式就要填一個節點,使用這個節點前面的那個反射的節點就可以不用了
<context:component-scan base-package="org.lxh"></context:component-scan>
base-package配置的是基本包,這個根據實際情況作修改,之後的工作就是在需要在要交給spring管理的接口和實現類加入上面四個註解中的一個 ,這裏的註解也可以配置bean的名稱,名稱的配置就不細說了
- package org.lxh.impl;
- import org.lxh.dao.TeacherDao;
- import org.springframework.stereotype.Service;
- @Service("TeacherManager")
- public class TeacherManager implements TeacherDao {
- public void tachc() {
- System.out.println("通過properties方式注入");
- }
- }
- package org.lxh.dao;
- import org.springframework.stereotype.Repository;
- @Repository
- public interface TeacherDao {
- public abstract void tachc();
- }
這樣的配置單元測試代碼應該如下
- package org.lxh.test;
- import org.lxh.dao.PersonDao;
- import org.lxh.dao.TeacherDao;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Testunit {
- @org.junit.Test public void TestUse(){
- //初始化spring容器
- //ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
- AbstractApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
- //取得業務bean
- TeacherDao dao=(TeacherDao)context.getBean("TeacherManager");
- dao.tachc();
- // dao.sayHello();
- // context.close();
- }
- }
下面是運行結果