概述
spring是一个针对bean的生命周期进行管理的轻量级容器,即管理类的实例化及属性的赋值的容器
spring使用初体验(基于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="demo1" class="com.Demo1"/>
</beans>
- JavaBean
public class Demo1 {
public void test1(){
System.out.println("Spring1");
}
}
- 测试
public class Spring1Test {
@Test
public void test1(){
/*
加载spring-config.xml配置文件,参数为配置文件路径
*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
/*
通过spring来获取demo1的实例,第一个参数为配置的id,第二个参数为目标class
*/
Demo1 demo1 = applicationContext.getBean("demo1", Demo1.class);
/*
测试是否生成实例
*/
demo1.test1();
}
}
属性赋值
- 配置文件
<?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="demo1" class="com.Demo1">
<property name="name" value="张三"/>
<property name="age" value="10" />
</bean>
</beans>
- JavaBean
public class Demo1 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void test1(){
System.out.println("Spring1");
}
public void test2(){
System.out.println("name:" + name + "\tage:" + age);
}
}
- 测试
@Test
public void test2(){
/*
加载spring-config.xml配置文件,参数为配置文件的路径
*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
/*
通过spring来获取demo1的实例,第一个参数为加载的id,第二个参数为目标class
*/
Demo1 demo1 = applicationContext.getBean("demo1", Demo1.class);
/*
测试属性命名是否成功
*/
demo1.test2();
}
Bean的管理
哪种类可以由Spring管理
- 在未传入参数时必须有无参构造器
- 不能是抽象类
- 对于私有拥有私有构造器的类,spring也可以加载(spring会创建一个可访问的构造器)
scope属性
singleton
默认值,在配置文件被加载时实例化,且只实例化一次
prototype
在getBean调用时实例化,每次调用getBean实例化一个新对象
属性的注入
set方法的注入
使用property标签,该标签会找到set方法来给属性赋值
- 简单类型(8种基本类型和String):使用name和value属性给属性赋值
<property name="" value="" />
- 对象/引用类型:通过ref属性来指定对象,目标对象也要在配置文件中配置好,ref属性值为目标bean的id
<bean id="bean1" class=""></bean>
<bean id="bean2" class="">
<property name="bean1" ref="bean1" />
</bean>
- 数组:通过array标签,对于简单类型,中使用value双标签,中间写值;对于复杂类型,使用ref单标签,bean属性指定目标bean的id
<array>
<value>value1</value>
<value>value2</value>
<value>value3</value>
<value>value4</value>
</array>
<array>
<ref bean="" />
<ref bean="" />
<ref bean="" />
</array>
- 集合:list标签,set标签与array使用方法一致,map标签中的entry标签属性key和value对应键值,properties类型与map相似,使用props标签内嵌套prop标签,key对应键,标签体对应值
<list>
<value></value>
</list>
<set>
<value></value>
</set>
<map>
<entry key="" value=""></entry>
</map>
<props>
<prop key="">value</prop>
</props>
构造方法的注入
带参构造方法给属性赋值,使用constructor-arg标签,value属性对应值,index属性对应多个参数时的参数索引
<constructor-arg value=""></constructor-arg>
针对接口编程(接口类型的注入)
在上层调用下层时,只调用下层的接口,对于具体的实现交给spring,在配置文件中,配置对应的实现类,然后进行对应接口属性的注入
好处
解耦,提高代码可维护性,需要改换实现时方便
如JDBC规范提供了一套接口(statement,resultset,connection),这些接口的实现交由数据库,所以在改换数据库时,改一个驱动即可
基于注解配置
使用context:component-scan标签,属性base-package为包名,来对包进行扫描,对加有注解的类进行管理
注入bean
- @Component:没有特殊意义
- @Controller:用于控制器类
- @Responsitory:用于DAO类
- @Service:用于Service类,服务层
注入属性
- @Value:简单属性
- @Autowired:对象属性,自动装配
- @Resource:对象属性,手动指定
spring集成mybatis环境搭建
依赖
数据源(druid),mybatis-spring,spring-jdbc,spring-test测试
数据源配置
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value=""/>
<property name="url" value=""/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
SqlSessionFactory定义
- 这种方式使用Mapper工厂实体的方式只配置单个Dao
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="roleDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.rbac.dao.RoleDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
- 这种方式使用Mapper扫配配置器的方式扫描包中的所有Dao,推荐
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.rbac.dao" />
</bean>
spring-test配置
在测试类上声明注解,对于内部的属性即可通过注入赋值,免去加载配置文件的步骤
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
配置事务
配置依赖spring-tx
方法一:在Service类上使用@EnableTranctionManagement,在类或方法上使用@Transactional
方法二:在applicationContext.xml中增加tx的命名空间,使用<tx:annotation-driven />
,在类或方法上使用@Transaction