spring中的注解和xml配置文件中配置对应
需要导入的jar
spring-context
spring-context-support
spring-test
commons-logging
bean
xml文件中的配置
id:对象名字唯一,不能使用特殊字符
name:对象名字
class:类的全限定名 包名.类名
init-method:初始化方法
destroy-method:销毁对象之前执行的方法
scope:作用域范围
prototype:多例
- 创建一次是一个新的对象
singleton:单例(懒汉式 饿汉式)
- 不管如何创建,只能是一个对象
lazy-init:是否延迟加载当前对象
只在单例模式下有效(多例模式下每次都需要调用无参构造对象,所以无效)
- 1.false 立即加载 加载当前spring配置文件时就创建对象
- 2.true 延迟加载 当第一次调用对象时加载
ref:引入参照对象
property 无参构造
name:对象中属性的名字
value:赋的值
constructor-arg有参构造
name注入:属性名
type注入:属性名的类型
index注入:类中第一个属性名为下标为 0
<bean id="provincesId"
name="provinces"
class="com.lanou.pojo.Provinces"
init-method="init"
destroy-method="destroy"
scope="singleton"
lazy-init="true"
>
<!--利用setter方法给对应的属性赋值-->
<property name="id" value="1"></property>
<property name="province" value="北京市"></property>
<property name="provinceid" value="110000"></property>
</bean>
<bean id="user" class="com.lanou.pojo.User" scope="singleton" lazy-init="true">
<constructor-arg name="id" value="123" type="int"></constructor-arg>
<constructor-arg name="username" value="说爱你" type="java.lang.String"></constructor-arg>
<constructor-arg name="password" value="love" type="java.lang.String"></constructor-arg>
<!--<constructor-arg index="0" value="121"></constructor-arg>-->
<!--<constructor-arg index="1" value="我爱你"></constructor-arg>-->
<!--<constructor-arg index="2" value="sss"></constructor-arg>-->
</bean>
复杂数据类型配置
<bean id="testCollection" class="com.lanou.pojo.TestCollection">
<!--对象类型 ref引入参照对象-->
<property name="user" ref="user"></property>
<!--数组-->
<property name="array">
<list>
<value>郑州</value>
<value>123</value>
<value>0.12</value>
</list>
</property>
<!--集合类型list-->
<property name="list">
<list>
<value>郑州</value>
<value>洛阳</value>
<value>开封</value>
</list>
</property>
<!--集合类型map-->
<property name="map">
<map>
<entry key="area" value="河南"></entry>
<entry key="fruit" value="芒果"></entry>
<entry key="user" value-ref="user"></entry>
</map>
</property>
<!--Properties类型赋值 key=value-->
<property name="properties">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost/mybatis</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
bean的注解
开启注解
<!--可省略不写 开启spring注解功能-->
<context:annotation-config></context:annotation-config>
<!--定义注解扫描的包-->
<context:component-scan base-package="com.lanou"></context:component-scan>
配置类
@Component 普通的javabean pojo包,util包
@Scope 单例多例模式值()
@Lazy延迟加载
配置属性赋值
@Value(value = “123”)
配置方法
@PostConstruct 初始化注解
@PreDestroy 销毁对象之前执行的方法
@Component(value = "user")
@Scope
public class User {
@Value(value = "123")
private int id;
@Value("张三")
private String username;
@Value("123456")
private String password;
层与层之间调用
xml配置
<bean id="provincesService" class="com.lanou.service.serviceImpl.ProvincesServiceImpl"></bean>
<bean id="provincesServlet" class="com.lanou.servlet.ProvincesServlet">
<property name="id" value="100"></property>
<property name="provincesService" ref="provincesService"></property>
</bean>
注解
控制层上注解类上面使用
@Controller(value = “userController”)
业务层类上使用的注解
@Service(“userService”)
属性上使用的注解
@Autowired //既可以通过类型,也可以通过名称注入
@Qualifier(value = “userDao2”)// 两个注解联合使用时,强制指定通过名称注入
@Resource(name = “userDao2”)
持久层类使用的注解
@Repository(“userDao”)
@RunWith(SpringJUnit4ClassRunner.class) 创建sprng容器
@ContextConfiguration(“classpath:applicationContext.xml”) 指定配置文件位置
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
aop 面向切面编程
xml文件中的配置
aop:config 定义切入点
aop:pointcut 切入点
id : 名称
expression : 表达式 找到包、类、方法
execution(* com.lanou.service..(…))
第一个* 方法的返回值类型
com.lanou.service 包名
第二个* 类名
第三个* 方法名
(…) 任意参数
aop:aspect 定义关联的切面
aop:before 前置通知
- method:切面中的前置方法
- pointcut-ref:切入点
aop:after 后置通知
aop:after-returning :后置通知 如果异常不返回值
aop:after-throwing :后置通知 异常返回
aop:around :环绕通知
<!--定义切面信息-->
<bean id="logAdvice" class="com.lanou.utils.LogAdvice2"></bean>
<!--定义切入点-->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.lanou.service.*.*(..))"/>
<!--定义关联的切面-->
<aop:aspect ref="logAdvice">
<aop:before method="before" pointcut-ref="pc"></aop:before>
<aop:after method="after" pointcut-ref="pc"></aop:after>
<aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning>
<aop:after-throwing method="afterException" pointcut-ref="pc"></aop:after-throwing>
<aop:around method="around" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
注解中的配置
导入aop需要的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
@Aspect:声明当前类是一个切面类
@Pointcut(“execution(* com.lanou.service..(…))”):切入点
@Before(“LogAdvice.pc()”): 前置通知方法
@After(“execution(* com.lanou.service..(…))”):后置通知方法 不管是否异常都返回
@AfterReturning(“execution(* com.lanou.service..(…))”):后置通知 如果异常不返回值
@AfterThrowing(“execution(* com.lanou.service..(…))”):后置通知 异常返回
<!--开启aop注解-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Component(value = "logAdvice")
//声明当前类是一个切面类
@Aspect
public class LogAdvice {
@Pointcut("execution(* com.lanou.service.*.*(..))")
public void pc(){
}
@Before("LogAdvice.pc()")
public void before(){
System.out.println("--前置通知----");
}
/*
切面方法
后置通知方法 不管是否异常都返回
*/
@After("execution(* com.lanou.service.*.*(..))")
public void after(){
System.out.println("--后置通知----");
}
/*
后置通知 如果异常不返回值
*/
@AfterReturning("execution(* com.lanou.service.*.*(..))")
public void afterReturning(){
System.out.println("---后置通知--无异常-");
}
/*
后置通知 异常返回
*/
@AfterThrowing("execution(* com.lanou.service.*.*(..))")
public void afterException(){
System.out.println("--后置通知--异常通知--");
}
/*
环绕通知
*/
@Around("execution(* com.lanou.service.*.*(..))")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕通知--前置通知");
Object object = point.proceed();//继续调用目标方法
System.out.println("环绕通知--后置通知");
return object;
}
}
事务的注解
导包
<!--导入事务的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
在xml中的配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybaits"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--事务切面bean-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for=""/>
</tx:attributes>
</tx:advice>
<!--配置事务切面-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pt" expression="execution(* com..service..*.*(..))"></aop:pointcut>
<!--配置切入点和切面关联-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
在类上使用
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.READ_COMMITTED,
timeout = 600
)