基于注解方式操作Bean组件管理
1.什么是注解
(1).注解是代码特殊标记,格式:@注解名(属性名=属性值,属性名=属性值)
(2.)使用注解,注解可以作用在类上,方法上,属性上
(3.)使用注解的目的:简化xml配置
2.Spring针对Bean管理中创建对象提供注解
(1)Component通用组件
(2)Service业务组件
(3)Controller控制器组件
(4)Repository持久层
*上述四个注解功能是一样的,都可以用来创建bean实例
3.基于注解方式实现对象创建步骤
1.引入依赖 spring-aop
2.开启组件扫描
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:component-scan base-package="com.dltt.*"/>
</beans>
4.在类中添加组件注解
1.在注解里面value属性设置的就是组件的id的值
2.value属性可以省略,默认值(默认组件名)为类名首字母小写
例如Emp》emp
5.基于注解方式实现属性注入
1.@Autowired:根据属性类型进行自动装配
2.@Qualifier:根据属性名称进行注入,若要使用需和@Autowired一起使用
3.@Resource:可以根据类型注入,也可以根据名称注入
4.@Value:注入普通类型属性
package com.dltt.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void addUser(){
System.out.println("添加了一名用户");
}
}
package com.dltt.service;
import com.dltt.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
// @Autowired
// @Resource//当不设置name属性是,采用的是按类型装配
@Resource(name = "userDao")//name属性设置注入组件的id
private UserDao userDao;
public void add(){
userDao.addUser();
}
}
package com.dltt.vo;
import org.springframework.stereotype.Component;
@Component
public class UserService1
{
public UserService1(){
System.out.println("通过无参构造实例化");
}
}
基于aop组件注解
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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring_05_aop_annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.21.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>
</project>
2.xml文件配置自动代理模式
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.dltt"/>
<!-- spring中使用aspectj包中的@Aspect注解标注当前组件为切面
如果要使用注解必须开启aspectJ的自动代理模式-->
<aop:aspectj-autoproxy/>
</beans>
3.java实现类实现访问注解
package com.dltt.service;
import org.springframework.stereotype.Component;
@Component
public class Target {
public void save(String name){
System.out.println("执行了save方法,保存了:"+name);
}
public int add(int a,int b){
return a+b;
}
public int addExp(int a, int b){
System.out.println(10/0);
return a+b;
}
}
package com.dltt.aspeect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("before")
@Aspect//设置为切面
public class TestBfore {
@Before("execution(void com.dltt.service.Target.save(String))")//前置访问
public void doBefore(JoinPoint jp){
System.out.println("接收参数"+jp.getArgs()[0]);
System.out.println("前置通知");
}
}
package com.dltt.aspeect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TextAfterReturning {
@AfterReturning(pointcut = "execution(int com.dltt.service.Target.add(int,int))",returning = "obj") //环绕访问
public void doAfterReturning(JoinPoint jp,Object obj){
System.out.println("返回值为"+obj);
System.out.println("后置通知");
}
}
事务注解:
@Transactional
1.可以在类上添加,这个类里面的所有方法都将被添加事务管理
2.也可以在方法上添加,只为这个方法添加事务管理
3.可以设置属性:1.propagation:事务的传播策略
2.isolation:隔离级别
3.timeout:超时时间
1.事务需要在规定时间内进行提交,如果不提交则回滚,默认值为-1,单位为秒
4.readOnly:是否只读
5.rollbackFor:
设置出现哪些异常时进行事务回滚
6.noRollbackFor
设置出现哪些异常不进行事务回滚
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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_09_tx_annotaion</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.bundles</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8_2</version>
</dependency>
</dependencies>
</pro
app.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 组件自动扫描-->
<context:component-scan base-package="com.dltt"/>
<!-- 数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mysql2209?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- jdbcTomplate组件注入-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--创建事务控制管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 开启aop动态代理-->
<aop:aspectj-autoproxy/>
</beans>