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();
- }
- }
下面是运行结果