> 文章列表 > Spring 02 -Spring依赖注入+Spring注解开发

Spring 02 -Spring依赖注入+Spring注解开发

Spring 02 -Spring依赖注入+Spring注解开发

spring依赖注入与注解开发

  • 1 依赖注入(DI)
    • 1.1 set方法注入
    • 1.2 构造方法注入
    • 1.3 复杂类型注入
    • 1.4 自定义类型的注入
  • 2 依赖注入案例
  • 3 Spring注解开发
    • 3.1 **开启注解扫描**
    • 3.2 Component 注解
    • 3.3 Autowired注解 (开发常用 )
    • 3.4 Resource注解

1 依赖注入(DI)

依赖注入:在Spring创建对象的同时,为其属性赋值,称之为依赖注入。

1.1 set方法注入

创建对象时,Spring工厂会通过Set方法为对象的属性赋值。

范例:定义一个Bean类型

public class User {private String name;private int age;private double money;private String hobby;private Date birthday;//省略set、get方法、构造方法
}

属性注入

<!-- 依赖注入:使用set方法进行注入 user.setName("") user.setBirthday(new Date())-->
<bean id="user" class="com.ying.pojo.User"><!--   简单类型注入(String+基本类型)     --><property name="name" value="张三"/><property name="age" value="10"/><property name="hobby" value="打篮球"/><property name="money" value="200.22"/><!--   对象类型注入(String+基本类型)     --><property name="birthday" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"><property name="year" value="122"/>
</bean>

1.2 构造方法注入

范例:定义一个Bean类型 提供构造方法

public class User {private String name;private int age;private double money;private String hobby;private Date birthday;//省略set、get方法、构造方法
}

构造方法注入

<!-- 依赖注入:使用构造方法进行注入  User user = new User("cxk",30,3000,"打篮球",new Date())-->
<bean id="user1" class="com.ying.pojo.User"><!--  通过构造方法名注入       --><!--        <constructor-arg name="name" value="李四"/>--><!--        <constructor-arg name="age" value="20"/>--><!--        <constructor-arg name="money" value="222.22"/>--><!--        <constructor-arg name="hobby" value="跳"/>--><!--        <constructor-arg name="birthday" ref="date"/>--><!--  通过构造方法的参数类型注入      --><!--        <constructor-arg type="java.lang.String" value="王五"/>--><!--        <constructor-arg type="int" value="30"/>--><!--        <constructor-arg type="double" value="333.33"/>--><!--        <constructor-arg type="java.lang.String" value="唱"/>--><!--        <constructor-arg type="java.util.Date" ref="date"/>--><!--  通过构造方法的下标注入      --><constructor-arg index="0" value="赵六"/><constructor-arg index="1" value="60"/><constructor-arg index="2" value="444.44"/><constructor-arg index="3" value="rap"/><constructor-arg index="4" ref="date"/>
</bean>

1.3 复杂类型注入

复杂类型指的是:list、set、map、array、properties等类型

定义复杂Bean类型

public class Person {private List<User> userList;private Map<String,String> map;private Set<String> set;private String[] arr;private Properties properties;//省略set、get方法、构造方法
}

给复杂类型注入属性值

<bean id="person" class="com.ying.pojo.Person"><property name="userList"><list><bean class="com.ying.pojo.User"></bean><bean class="com.ying.pojo.User"></bean><bean class="com.ying.pojo.User"></bean></list></property><property name="set"><set><value>1</value><value>2</value><value>3</value></set></property><property name="arr"><array><value>a</value><value>b</value><value>c</value></array></property><property name="map"><map><entry key="k1" value="v1"></entry><entry key="k2" value="v2"></entry><entry key="k3" value="v3"></entry></map></property><property name="properties"><props><prop key="driver">com.mysql.jdbc.Dirver</prop><prop key="url">jdbc:mysql:///java2101</prop></props></property>
</bean>

1.4 自定义类型的注入

范例:在UserServiceImpl中注入UserDaoImpl类型的属性

public class UserServiceImpl {private UserDaoImpl userDao;public void setUserDao(UserDaoImpl userDao) {this.userDao = userDao;}public void addUser(){userDao.addUser();}
}
<!--  属性注入演示  -->
<!-- 创建UserDaoImpl对象    -->
<bean id="userDao" class="com.ying.dao.UserDaoImpl"></bean>
<!-- 创建UserServiceImpl对象    -->
<bean id="userService" class="com.ying.service.UserServiceImpl"><property name="userDao" ref="userDao"></property>
</bean>

2 依赖注入案例


Spring整合DBUtils实现JDBC开发

EmpServiceImpl

public class EmpServiceImpl {private EmpDaoImpl empDao;public void setEmpDao(EmpDaoImpl empDao) {this.empDao = empDao;}public List<Emp> getAll(){try {return empDao.getAll();} catch (SQLException throwables) {throwables.printStackTrace();}return null;}
}

EmpDaoImpl

public class EmpDaoImpl {private QueryRunner queryRunner;public EmpDaoImpl(QueryRunner queryRunner) {this.queryRunner = queryRunner;}public List<Emp> getAll() throws SQLException {return queryRunner.query("select * from emp ",new BeanListHandler<Emp>(Emp.class));}
}

依赖注入配置

<!-- 使用Spring整合DBUtils工具类   -->
<!-- 创建EmpServiceImpl对象   -->
<bean id="empService" class="com.ying.service.EmpServiceImpl"><property name="empDao" ref="empDao"/>
</bean>
<!-- 创建EmpDaoImpl对象   -->
<bean id="empDao" class="com.ying.dao.EmpDaoImpl"><constructor-arg name="queryRunner" ref="queryRunner"></constructor-arg>
</bean>
<!-- 创建QueryRunner对象   -->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"><constructor-arg name="ds" ref="dataSource"/>
</bean>
<!-- 创建DruidDataSource对象   -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--   注入DruidDataSource连接池的配置信息     --><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql:///java2101"/><property name="username" value="root"/><property name="password" value="123456"/><!--        <property name="initialSize" value="40"/>--><!--        <property name="maxActive" value="100"/>--><!--        <property name="minIdle" value="20"/>--><!--        <property name="maxWait" value="600000"/>-->
</bean>

测试

@Test
public void test05(){//创建Spring容器对象ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");EmpServiceImpl empService = ac.getBean("empService", EmpServiceImpl.class);List<Emp> empList = empService.getAll();empList.stream().forEach(System.out::println);
}

3 Spring注解开发

3.1 开启注解扫描

<!--  开启注解扫描  -->
<context:component-scan base-package="com.ying"/>

3.2 Component 注解

用于替换自建类型组件的 <bean…>标签;可以更快速的声明bean

@Component 注解的含义:创建类的对象,相当于xml中配置的bean标签

  • @Controller 针对于控制器层的类的注解
  • @Service 针对于业务层的类的注解
  • @Repository 针对持久层的类的注解 Dao数据访问层
@Component
//@Service("empService")
//@Controller("empService")
//@Repository("empService")
public class EmpServiceImpl implements EmpService{ 
}

3.3 Autowired注解 (开发常用 )

主要用于属性注入,Spring

  • @Autowired 自动装配:将对象属性自动进行注入(类型注入)

  • @Qualifier(“empDaoImpl1”) 限定要自动注入的bean的id,一般和@Autowired联用

注意:@Autowired先会按照类型注入,如果这个类有多个实例,那么再按照名称注入,如果有多个实例那么需要使用Qualifier进行指定

@Component
public class EmpServiceImpl implements EmpService{@Autowired@Qualifier("empDaoImpl1")//@Resource(name="empDaoImpl2")private EmpDao empDao;public void addEmp(){empDao.addEmp();}
}

3.4 Resource注解

主要用于属性注入,jdk

@Resource 自动装配:将对象属性自动进行注入(名称注入)

注意:默认按照名称进行装配,如果找到指定的名称则直接注入。如果没有找到,那么再按类型称注入,如果有多个实例那么需要使用name属性进行指定

@Component//@Controller   @Service  @Repository 
public class EmpServiceImpl implements EmpService{@Resource(name="empDaoImpl2")private EmpDao empDao;public void addEmp(){empDao.addEmp();}
}