Spring
Spring
注解开发
注解开发定义bean
@Component
@Repository
@Service
都可以进行注解
package com.note_test.service.impl;
import com.note_test.dao.BookDao;
import com.note_test.service.BookService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
//@Component("bookService")
@Service("bookService")
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void save() {
System.out.println("BookService...");
bookDao.save();
}
}
package com.note_test.dao.impl;
import com.note_test.dao.BookDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
//@Component("bookDao")
@Repository("bookDao")
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("BookDao...");
}
}
<?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:component-scan base-package="com.note_test" />
</beans>
纯注解开发
配置类:
package com.note_test.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
@Configurable
@ComponentScan({"com.note_test.dao","com.note_test.service"})
public class SpringConfig {
}
package com.note_test;
import com.note_test.config.SpringConfig;
import com.note_test.dao.BookDao;
import com.note_test.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Appnotation {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
System.out.println(bookDao);
bookDao.save();
System.out.println("-----------------------");
BookService bookService = (BookService) ctx.getBean("bookService");
System.out.println(bookService);
// bookService.save();
}
}
注解开发
bean管理
bean作用范围
bean生命周期
@PostConstruct、@PreDestroy
@Repository("bookDao")
public class BookDaoImpl implements BookDao {
private String name;
@Override
public void save() {
System.out.println("BookDao..." + name);
}
@PostConstruct
public void init(){
System.out.println("init...");
}
@PreDestroy
public void destroy(){
System.out.println("destroy...");
}
}
依赖注入
自动装配
@Autowired
@Service("bookService")
public class BookServiceImpl implements BookService {
@Autowired
@Qualifier("bookDao")//自动装配时指定装配对象的名称
private BookDao bookDao;
@Override
public void save() {
System.out.println("BookService...");
bookDao.save();
}
}
@Qualifier指定加载某一个bean
简单类型自动装配
@Value
使用@PropertySource({“classpath:jdbc.properties”})加载配置文件
package com.note_test.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
@Configurable
@ComponentScan("com.note_test")
@PropertySource({"classpath:jdbc.properties"})
public class SpringConfig {
}
@Repository("bookDao")
//@Scope("singleton")
public class BookDaoImpl implements BookDao {
@Value("${name}")
private String name;
@Override
public void save() {
System.out.println("BookDao..." + name);
}
}
第三方bean管理
package com.three.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configurable
@ComponentScan("com.three.dao")
@Import({JdbcConfig.class})
public class SpringConfig {
}
package com.three.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.three.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("com.mysql.jdbc.Driver")
private String dricer;
@Value("jdbc:mysql://localhost:3306/db1")
private String url;
@Value("root")
private String name;
@Value("123456")
private String password;
@Bean
public DataSource dataSource(BookDao bookDao){//引用类型注入
System.out.println(bookDao);
//简单类型注入
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(dricer);
ds.setUrl(url);
ds.setUsername(name);
ds.setPassword(password);
return ds;
}
}
package com.three.dao.impl;
import com.three.dao.BookDao;
import org.springframework.stereotype.Component;
@Component("bookDao")
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("BookDaoImpl....");
}
}
梳理课程
Spring整合Mybatis
public class MybatisConfig {
// <!--起别名:在userMapper中可以直接使用<select id="selectAll" resultType="user">-->
// <typeAliases>
// <package name="com.test.pojo"/>
// </typeAliases>
// <!--
// environments:配置数据库连接坏境信息。可以配置多个environments,通过不同的default属性切换不同的
// -->
// <environments default="development">
// <environment id="development">
// <transactionManager type="JDBC"/>
// <dataSource type="POOLED">
//<!-- 数据库连接信息-->
// <property name="driver" value="com.mysql.jdbc.Driver"/>
// <property name="url" value="jdbc:mysql:///db1?useSSL=false"/>
// <property name="username" value="root"/>
// <property name="password" value="123456"/>
// </dataSource>
// </environment>
// </environments>
@Bean
public SqlSessionFactoryBean sqlSessionFactor(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.test.pojo");
ssfb.setDataSource(dataSource);
return ssfb;
}
// <mappers>
// <!--加载sql映射文件-->
//<!-- <mapper resource="com/test/mapper/userMapper.xml"/>-->
//
// <!--Mapper代理方式-->
// <package name="com.test.mapper"/>
// </mappers>
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.test.dao");
return msc;
}
}
在test中测试spring
Spring事务
- 事务的作用:在数据层保证一系列的数据库操作同成功同失败
- Spring事务作用:在数据层或业务层保障一系列的数据库操作同成功同失败
步骤
Spring事务角色
事务角色
- 事务管理员:发起事务方,在Spring中通常值业务层开启事务的方法(开启事务T)
- 事务协调员:加入事务方,在Spring中通常指代数据层的方法就,也可以是业务层方法(加入事务T)
事务相关配置
事务传播行为
- 事务传播行为:事务协调员对事务管理员所携带事务的处理态度