3.基于注解的IOC配置
学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。4
3.1.创建工程
3.1.1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring</artifactId>
<groupId>com.by</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.by</groupId>
<artifactId>Spring_IOC_Annotation</artifactId>
<properties>
<!-- 项目源码及编译输出的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 项目编译JDK版本 -->
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring常用依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
</project>
3.1.2.dao
package com.by.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void addUser(){
System.out.println("insert into tb_user......");
}
}
3.1.3.service
package com.by.service;
import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class UserServiceImpl implements UserService{
//@Resource(name="userDaoImpl") //按名称从容器中获得bean,并注入给userDao
@Autowired
private UserDao userDao;
@Value("${name}")//${name}:从ioc容器中获得“name”的值,@Value():并注入给name
private String name;
@Value("${age}")
private Integer age;
@Override
public void addUser() {
System.out.println(userDao+" "+name+" "+age);
userDao.addUser();
}
}
3.2.IOC
3.2.1.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!--加载config.properties配置文件,并把配置文件中的数据存到IOC容器中-->
<context:property-placeholder location="config.properties"></context:property-placeholder>
<!--
告诉spring,容器启动时要扫描的包,且spring会依据注解创建对象并存到IOC容器中
-->
<context:component-scan base-package="com.by"></context:component-scan>
</beans>
3.2.2.dao
package com.by.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void addUser(){
System.out.println("insert into tb_user......");
}
}
3.2.3.service
package com.by.service;
import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class UserServiceImpl implements UserService{
//@Resource(name="userDaoImpl") //按名称从容器中获得bean,并注入给userDao
@Autowired
private UserDao userDao;
@Value("${name}")//${name}:从ioc容器中获得“name”的值,@Value():并注入给name
private String name;
@Value("${age}")
private Integer age;
@Override
public void addUser() {
System.out.println(userDao+" "+name+" "+age);
userDao.addUser();
}
}
3.3.DI
3.3.1.service
package com.by.service;
import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class UserServiceImpl implements UserService{
//@Resource(name="userDaoImpl") //按名称从容器中获得bean,并注入给userDao
@Autowired
private UserDao userDao;
@Value("${name}")//${name}:从ioc容器中获得“name”的值,@Value():并注入给name
private String name;
@Value("${age}")
private Integer age;
@Override
public void addUser() {
System.out.println(userDao+" "+name+" "+age);
userDao.addUser();
}
}
3.3.2.测试
package com.by.web;
import com.by.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class client {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = ac.getBean("userServiceImpl", UserService.class);
userService.addUser();
for (int i = 0; i < 5; i++) {
UserService userServicei = ac.getBean("userServiceImpl", UserService.class);
System.out.println(userServicei);
}
}
}
3.3.常用注解
3.3.1.用于创建对象的
以下四个注解的作用及属性都是一模一样的,都是针对一个的衍生注解只不过是提供了更加明确的语义化。
3.3.1.1.@Controller
-
作用:
把资源交给spring来管理,相当于:
<bean id="" class="">
;一般用于表现层。 -
属性:
value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;
3.3.1.2.@Service
-
作用:
把资源交给spring来管理,相当于:
<bean id="" class="">
;一般用于业务层。 -
属性:
value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;
-
案例
//@Service("userService")声明bean,且id="userServiceImpl"
@Service//声明bean,且id="userServiceImpl"
public class UserServiceImpl implements UserService {
...
}
3.3.1.3.@Repository
-
作用:
把资源交给spring来管理,相当于:
<bean id="" class="">
;一般用于持久层。 -
属性:
value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;
-
案例
//@Repository("userDaoImpl")声明bean,且id="userDaoImpl"
@Repository//声明bean,且id="userDaoImpl"
public class UserDaoImpl implements UserDao {
@Override
public void addUser(){
System.out.println("insert into tb_user......");
}
}
3.3.1.4.@Component
-
作用:
把资源交给spring来管理,相当于:
<bean id="" class="">
;通用。 -
属性:
value:指定bean的id;如果不指定value属性,默认bean的id是当前类的类名,首字母小写;
3.3.1.5.@Scope
-
作用:
指定bean的作用域范围。
-
属性:
value:指定范围的值,singleton prototype request session。
3.3.2.用于属性注入的
以下四个注解的作用相当于:<property name="" ref="">
。
3.3.2.1.@Autowired
-
作用:
自动按照类型注入。set方法可以省略。
-
案例:
@Service
public class UserServiceImpl implements UserService {
@Autowired //注入类型为UserDAO的bean
private UserDao userDao;
public void addUser(){
userDao.addUser();
}
}
3.3.2.1.@Resource
-
作用:
自动按照名字注入。set方法可以省略。
-
属性:
name:指定bean的id。
- 案例:
@Service
public class UserServiceImpl implements UserService {
@Resource(name="userDaoImpl")//注入id=“userDaoImpl”的bean
private UserDao userDao;
public void addUser(){
userDao.addUser();
}
}
3.3.2.1.@Value
-
作用:
注入基本数据类型和String类型数据的
-
属性:
value:用于指定值
- 案例一
@Service
public class UserServiceImpl implements UserService {
@Resource(name="userDaoImpl") //注入id=“userDaoImpl”的bean
private UserDao userDao;
@Value("张三")//注入String
private String name;
@Value("18")//注入Integer
private Integer age;
public void addUser(){
System.out.println(name+","+age);
userDao.addUser();
}
}
- 案例二
- 创建config.properties
name=张三
age=18
- 加载配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!--加载config.properties-->
<context:property-placeholder location="config.properties"/>
<context:component-scan base-package="com.by"></context:component-scan>
</beans>
- 注入属性值
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Value("${name}")//注入String
private String name;
@Value("${age}")//注入Integer
private Integer age;
public void addUser() {
System.out.println(name+","+age);
userDao.addUser();
}
}