Spring个人笔记


给个中文版的spring手册 版本稍低 : Spring中文版

1、IOC

1.1、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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
   <!--<import/>元素从另一个文件中加载 bean 定义-->
   <import resource="services.xml"/>
   
</beans>

id 属性是用于标识单个 bean 定义的字符串。 class 属性定义 Bean 的类型,并使用完全限定的类名id 属性的值是指协作对象。

1.2、使用容器

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

1.3、实例化Bean

bean标签
参数名说明
id容器中提供的唯一标识。用于获取对象。
class指定类的全限定类名。用于反射创建对象。
scope指定对象的作用范围。singleton单例 prototype多例
init-method指定类中的初始化方法名称。
destroy-method指定类中销毁方法名称。

1.3.1 构造函数实例化

<bean id="exampleBean" class="examples.ExampleBean"/>

1.3.2 静态工厂方法实例化

<bean id="clientService"
    class="examples.ClientService"
    factory-method="createInstance"/>
//创建静态工厂
public class ClientService {
    private static ClientService clientService = new ClientService();
    private ClientService() {}

    public static ClientService createInstance() {
        return clientService;
    }
}

1.3.3 实例工厂方法实例化

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>
public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl();

    public ClientService createClientServiceInstance() {
        return clientService;
    }
}

1.4、Dependencies 依赖注入

依赖注入(DI)是一个过程,在此过程中,对象仅通过构造函数参数,工厂方法的参数或在构造对象实例后在对象实例上设置的属性来定义其依赖关系,即与它们一起使用的其他对象或从工厂方法返回。然后,容器在创建 Bean 时“注入”那些依赖项。

1.4.1 构造函数的依赖项注入

默认使用无参构造函数

<!--默认使用无参构造函数-->
<bean id="exampleBean" class="examples.ExampleBean"/>
标签 constructor-arg
参数名说明
index指定参数在构造函数参数列表的索引位置
type指定参数在构造函数中的数据类型
name指定参数在构造函数中的名称
value赋值基本数据类型和 String 类型
ref能赋的值是其他 bean 类型,必须是在配置文件中配置过的 bean
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
</bean>

<bean id="foo" class="x.y.Foo">
	<constructor-arg ref="bar"/>
</bean>

<bean id="bar" class="x.y.Bar"/>

1.4.2 Setter 的依赖项注入

property标签
参数名说明
name找类中 setXXX 方法
value赋值基本数据类型和 String 类型 ,使用最多
ref能赋的值是其他 bean 类型,必须是在配置文件中配置过的 bean
<bean id="exampleBean" class="examples.ExampleBean">
    <!-- setter injection using the neater ref attribute -->
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
</bean>

<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

1.4.3 p 名称空间注入数据(本质还是调用 set 方法)

需要引入命名空间  xmlns:p="http://www.springframework.org/schema/p"
<beans xmlns="http://www.springframework.org/schema/beans"
			 xmlns:p="http://www.springframework.org/schema/p"
			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">
 
<bean id="accountService" 
 class="com.xyulu.service.impl.AccountServiceImpl4"
 p:name="test" p:age="21" p:birthday-ref="now"/>
</beans>

1.4.4 注入集合属性

注入集合数据

List 结构:array,list,set
Map 结构:map,entry,props,prop

<bean id="accountService" class="com.xyulu.service.impl.AccountServiceImpl">
<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
<!-- 注入 set 集合数据 --> 
<property name="myStrs"> 
	<set>
		<value>AAA</value>
		<value>BBB</value> 
		<value>CCC</value>
	</set>
</property>
<!-- 注入 list 集合数据 --> 
<property name="myList"> 
	<list> 
		<value>AAA</value>
		<value>BBB</value>
		<value>CCC</value>
	</list>
</property>
<!-- 注入 Map 数据 --> 
<property name="myProps"> 
	<map>
		<entry key="testA" value="aaa"></entry>
	</map>
</property>

1.5 基于注解的IOC

注解注入是在 XML 注入之前执行的,因此对于通过两种方法连接的属性,后一种配置将覆盖前者。

(请注意包含context名称空间)

1.如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。

2.如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

3.如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

4.如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

使用context:annotation-config/ 就可以隐式地自动向Spring容器注册4个BeanPostProcessor

<?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"
    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:annotation-config/>

</beans>

1.5.1 常用注解

注解说明
@Component使用在类上用于实例化Bean
@Controller使用在web层类上用于实例化Bean
@Service使用在service层类上用于实例化Bean
@Repository使用在dao层类上用于实例化Bean
@Autowired使用在字段上用于根据类型依赖注入
@Qualifier结合@Autowired一起使用用于根据名称进行依赖注入
@Resource相当于@Autowired+@Qualifier,按照名称进行注入
@Value注入普通属性
@Scope标注Bean的作用范围
@PostConstruct使用在方法上标注该方法是Bean的初始化方法
@PreDestroy使用在方法上标注该方法是Bean的销毁方法

注意:

使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。

<!--注解的组件扫描-->
<context:component-scan base-package="com.xyulu"></context:component-scan>
使用 < context:component-scan/> 后,就可以将 < context:annotation-config/> 移除了

1.5.2、Spring新注解

非自定义的Bean的配置:

加载properties文件的配置:context:property-placeholder

组件扫描的配置:context:component-scan

引入其他文件:

注解说明
@Configuration用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中的 <context:component-scan base-package=“com.xyulu”/>一样
@Bean使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource用于加载.properties 文件中的配置
@Import用于导入其他配置类

2、Spring集成Junit步骤

①导入spring集成Junit的坐标

<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

②使用@Runwith注解替换原来的运行期

@RunWith(SpringJUnit4ClassRunner.class)
public class SpringJunitTest {
}

③使用@ContextConfiguration指定配置文件或配置类

@RunWith(SpringJUnit4ClassRunner.class)
//加载spring核心配置文件
//@ContextConfiguration(value = {"classpath:applicationContext.xml"})
//加载spring核心配置类
@ContextConfiguration(classes = {SpringConfiguration.class})
public class SpringJunitTest {
}

④使用@Autowired注入需要测试的对象

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class SpringJunitTest {
    @Autowired
    private UserService userService;
}

⑤创建测试方法进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})public class SpringJunitTest {
    @Autowired
    private UserService userService;
    @Test
    public void testUserService(){
   	 userService.save();
    }
}

Spring集成Junit步骤

①导入spring集成Junit的坐标

②使用@Runwith注解替换原来的运行期

③使用@ContextConfiguration指定配置文件或配置类

④使用@Autowired注入需要测试的对象

⑤创建测试方法进行测试

3、AOP面向切面编程

3.1、 AOP 的作用及其优势

作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强

优势:减少重复代码,提高开发效率,并且便于维护

3.2、基于 XML 的 AOP 开发

3.2.1、开发步骤

①导入 AOP 相关坐标

<!--导入spring的context坐标,context依赖aop-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.5.RELEASE</version>
</dependency>
<!-- aspectj的织入 -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.13</version>
</dependency>

②创建目标接口和目标类(内部有切点)

public interface TargetInterface {
    public void method();
}

public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}

③创建切面类(内部有增强方法)

public class MyAspect {
    //前置增强方法
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

④将目标类和切面类的对象创建权交给 spring

<!--配置目标类-->
<bean id="target" class="com.xyulu.aop.Target"></bean>
<!--配置切面类-->
<bean id="myAspect" class="com.xyulu.aop.MyAspect"></bean>

⑤在 applicationContext.xml 中配置织入关系

导入aop命名空间

<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/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
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

⑤在 applicationContext.xml 中配置织入关系

配置切点表达式和前置增强的织入关系

<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <!--配置Target的method方法执行时要进行myAspect的before方法前置增强-->
        <aop:before method="before" pointcut="execution(public void com.xyulu.aop.Target.method())"></aop:before>
    </aop:aspect>
</aop:config>

3.2.2 切点表达式的写法

表达式语法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略

  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意

  • 包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类

  • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

例如:

execution(public void com.xyulu.aop.Target.method())	
execution(void com.xyulu.aop.Target.*(..))
execution(* com.xyulu.aop.*.*(..))
execution(* com.xyulu.aop..*.*(..))
execution(* *..*.*(..))

3.2.3 通知的类型

通知的配置语法:

<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>

3.2.4 切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <aop:pointcut id="myPointcut" expression="execution(* com.xyulu.aop.*.*(..))"/>
        <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
    </aop:aspect>
</aop:config>

3.3 知识要点

  • aop织入的配置
<aop:config>
    <aop:aspect ref=“切面类”>
        <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
    </aop:aspect>
</aop:config>
  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知
  • 切点表达式的写法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))

3.4 基于注解的 AOP 开发

注解类型

在这里插入图片描述

①创建目标接口和目标类(内部有切点)

public interface TargetInterface {
    public void method();
}

public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}

②创建切面类(内部有增强方法)

public class MyAspect {
    //前置增强方法
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

③将目标类和切面类的对象创建权交给 spring

@Component("target")
public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

④在切面类中使用注解配置织入关系

@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("execution(* com.xyulu.aop.*.*(..))")
    public void before(){
        System.out.println("前置代码增强.....");
    }
}

⑤在配置文件中开启组件扫描和 AOP 的自动代理

<!--组件扫描-->
<context:component-scan base-package="com.xyulu.aop"/>

<!--aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3.5 基于注解的声明式事务控制

3.5.1 使用注解配置声明式事务控制

  1. 编写 AccoutDao
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }
    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}
  1. 编写 AccoutService
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}
  1. 编写 applicationContext.xml 配置文件
<!—之前省略datsSource、jdbcTemplate、平台事务管理器的配置-->
<!--组件扫描-->
<context:component-scan base-package="com.xyulu"/>
<!--事务的注解驱动-->
<tx:annotation-driven/>

3.5.2 注解配置声明式事务控制解析

①使用 @Transactional 在需要进行事务控制的类或是方法上修饰,注解可用的属性同 xml 配置方式,例如隔离级别、传播行为等。

②注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置。

③使用在方法上,不同的方法可以采用不同的事务参数配置。

④Xml配置文件中要开启事务的注解驱动<tx:annotation-driven />

3.5.3 知识要点

注解声明式事务控制的配置要点

  • 平台事务管理器配置(xml方式)

  • 事务通知的配置(@Transactional注解配置)

  • 事务注解驱动的配置 tx:annotation-driven/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值