IOC/DI配置管理第三方bean 案例:配置Druid 环境准备
创建maven项目
pom.xml添加依赖 spring-context
resources下添加 applicationContext.xml
编写运行类 ApplicationContext ctx
pom.xml导入 druid
依赖 配置类配置第三方bean <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/spring_db" /> <property name ="username" value ="root" /> <property name ="password" value ="950827" /> </bean > </beans >
从IOC容器中获取对应的bean对象 public class App { public static void main (String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml" ); DataSource dataSource = (DataSource) ctx.getBean("dataSource" ); System.out.println(dataSource); } }
注意事项
对于新的技术,不知道具体的坐标该如何查找?
百度
从mvn的仓库https://mvnrepository.com/
中进行搜索
报的错为ClassNotFoundException ,具体的类为com.mysql.jdbc.Driver
错误的原因是缺少mysql的驱动包
在pom.xml把驱动包引入即可
加载properties文件 原因: 属性写在Spring的配置文件中不利于后期维护 —> 将这些值提取到一个外部的properties配置文件中
具体步骤:
resources下创建一个jdbc.properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db jdbc.username=root jdbc.password=root
开启context
命名空间、加载properties配置文件、完成属性注入 <?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:property-placeholder location ="jdbc.properties" /> <bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource" > <property name ="driverClassName" value ="${jdbc.driver}" /> <property name ="url" value ="${jdbc.url}" /> <property name ="username" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> </bean > </beans >
多个property文件需要加载时 <context:property-placeholder location ="jdbc.properties,jdbc2.properties" system-properties-mode ="NEVER" /> <context:property-placeholder location ="*.properties" system-properties-mode ="NEVER" /> <context:property-placeholder location ="classpath:*.properties" system-properties-mode ="NEVER" /> <context:property-placeholder location ="classpath*:*.properties" system-properties-mode ="NEVER" /> </beans >
核心容器 简单的理解为ApplicationContext
容器的创建方式 ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml" );ApplicationContext ctx = new FileSystemXmlApplicationContext ("D:\\workspace\\spring\\spring_10_container\\src\\main\\resources\\applicationContext.xml" );
bean的三种获取方式 BookDao bookDao = (BookDao) ctx.getBean("bookDao" );BookDao bookDao = ctx.getBean("bookDao" ,BookDao.class);BookDao bookDao = ctx.getBean(BookDao.class);
容器类层次结构
BeanFactory —额额不太懂 使用BeanFactory来创建IOC容器 public class AppForBeanFactory { public static void main (String[] args) { Resource resources = new ClassPathResource ("applicationContext.xml" ); BeanFactory bf = new XmlBeanFactory (resources); BookDao bookDao = bf.getBean(BookDao.class); bookDao.save(); } }
总结
使用BeanFactory创建的容器是延迟加载
使用ApplicationContext创建的容器是立即加载
具体BeanFactory如何创建只需要了解即可
IOC/DI注解开发 注解开发定义bean
环境准备
删除原XML配置的<bean>
标签
Dao上添加@Component
注解
配置Spring的注解包扫描 @ComponentScan
ServiceImpl类上也添加@Component
运行行为类测试
@Controller
、@Service
、@Repository
纯注解开发模式
将配置文件applicationContext.xml删除掉,使用类来替换
【❗更新】环境准备
创建maven
pom.xml
springconfig【刚学的注解开发定义bean】
添加类
创建一个配置类SpringConfig
、标识该类为配置类、用注解替换包扫描配置 @Configuration @ComponentScan("com.itheima") public class SpringConfig {}
创建运行类并执行 public class AppForAnnotation { public static void main (String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext (SpringConfig.class); BookDao bookDao = (BookDao) ctx.getBean("bookDao" ); System.out.println(bookDao); BookService bookService = ctx.getBean(BookService.class); System.out.println(bookService); } }
注解开发bean的作用范围和生命周期管理 @Scope("prototype") @Repository public class BookDaoImpl implements BookDao { public void save () { System.out.println("book dao save ..." ); } @PostConstruct public void init () { System.out.println("init ..." ); } @PreDestroy public void destroy () { System.out.println("destroy ..." ); } }
注解对应关系图
注解开发依赖注入 bean和service的关系
之前的配置文件application配置好的bean和service的关系还没用注解开发,所以bookDao对象为Null,调用其save方法就会报空指针异常 。
解决办法:在BookServiceImpl类的bookDao属性上添加@Autowired
注解@Service public class BookServiceImpl implements BookService { @Autowired private BookDao bookDao; public void save () { System.out.println("book service save ..." ); bookDao.save(); } }
按照名称注入
如果Dao接口类有多个实现类,解决方案:按照名称注入
DaoImpl文件 @Repository("bookDao") public class BookDaoImpl implements BookDao { public void save () { System.out.println("book dao save ..." ); } } @Repository("bookDao2") public class BookDaoImpl2 implements BookDao { public void save () { System.out.println("book dao save ...2" ); } }
ServiceImpl文件 @Service public class BookServiceImpl implements BookService { @Autowired @Qualifier("bookDao1") private BookDao bookDao; public void save () { System.out.println("book service save ..." ); bookDao.save(); } }
简单数据类型注入 DaoImpl文件 @Repository("bookDao") public class BookDaoImpl implements BookDao { **private String name;** public void save () { System.out.println("book dao save ..." + name); } }
ServiceImpl文件 @Repository("bookDao") public class BookDaoImpl implements BookDao { **@Value("itheima") ** private String name; public void save () { System.out.println("book dao save ..." + name); } }
注解读取properties配置文件 resource下准备properties文件 springconfig加载properties配置文件 @Configuration @ComponentScan("com.itheima") @PropertySource("jdbc.properties") public class SpringConfig {}
DaoImpl读取配置文件中的内容 @Repository("bookDao") public class BookDaoImpl implements BookDao { **@Value("${name}") ** private String name; public void save () { System.out.println("book dao save ..." + name); } }
IOC/DI注解开发管理第三方bean - 第三方的类都是在jar包中,我们没有办法在类上面添加注解?
案例:注解开发管理第三方bean 导入对应的jar包 在配置类中添加一个方法、添加@Bean
注解 @Configuration public class SpringConfig { **@Bean ** public DataSource dataSource () { DruidDataSource ds = new DruidDataSource (); ds.setDriverClassName("com.mysql.jdbc.Driver" ); ds.setUrl("jdbc:mysql://localhost:3306/spring_db" ); ds.setUsername("root" ); ds.setPassword("950827" ); return ds; } }
从IOC容器中获取对象并打印 public class App { public static void main (String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SpringConfig.class); DataSource dataSource = ctx.getBean(DataSource.class); System.out.println(dataSource); } }
引入外部配置类 新建一个JdbcConfig
配置类 public class JdbcConfig { @Bean public DataSource dataSource () { DruidDataSource ds = new DruidDataSource (); ds.setDriverClassName("com.mysql.jdbc.Driver" ); ds.setUrl("jdbc:mysql://localhost:3306/spring_db" ); ds.setUsername("root" ); ds.setPassword("root" ); return ds; } }
在Spring配置类中引入 @import
@Configuration @Import({JdbcConfig.class}) public class SpringConfig {}
注解开发实现为第三方bean注入资源 简单数据类型 —— 使用@Value
注解引入值
public class JdbcConfig { @Value("com.mysql.jdbc.Driver") private String driver; @Value("jdbc:mysql://localhost:3306/spring_db") private String url; @Value("root") private String userName; @Value("password") private String password; @Bean public DataSource dataSource () { DruidDataSource ds = new DruidDataSource (); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; } }
引用数据类型 在SpringConfig中扫描BookDao —— @ComponentScan("com.itheima.dao")
在JdbcConfig类的方法上添加参数 @Bean public DataSource dataSource (BookDao bookDao) { **System.out.println(bookDao);** DruidDataSource ds = new DruidDataSource (); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; }
注解开发总结
spring整合! –整合Mybatis 环境准备 数据库表 create database spring_db character set utf8;use spring_db; create table tbl_account( id int primary key auto_increment, name varchar (35 ), money double );
创建项目导入jar包** <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > 5.2.10.RELEASE</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > 1.1.16</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.6</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.47</version > </dependency > </dependencies >
根据表创建模型类 public class Account implements Serializable { private Integer id; private String name; private Double money; }
创建Dao接口 public interface AccountDao { @Insert("insert into tbl_account(name,money)values(#{name},#{money})") void save (Account account) ; @Delete("delete from tbl_account where id = #{id} ") void delete (Integer id) ; @Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ") void update (Account account) ; @Select("select * from tbl_account") List<Account> findAll () ; @Select("select * from tbl_account where id = #{id} ") Account findById (Integer id) ; }
创建Service接口和实现类 public interface AccountService { void save (Account account) ; void delete (Integer id) ; void update (Account account) ; List<Account> findAll () ; Account findById (Integer id) ; } @Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; public void save (Account account) { accountDao.save(account); } public void update (Account account) { accountDao.update(account); } public void delete (Integer id) { accountDao.delete(id); } public Account findById (Integer id) { return accountDao.findById(id); } public List<Account> findAll () { return accountDao.findAll(); } }
添加jdbc.properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql: jdbc.username=root jdbc.password=root
添加Mybatis核心配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" ><configuration > <properties resource ="jdbc.properties" > </properties > <typeAliases > <package name ="com.itheima.domain" /> </typeAliases > <environments default ="mysql" > <environment id ="mysql" > <transactionManager type ="JDBC" > </transactionManager > <dataSource type ="POOLED" > <property name ="driver" value ="${jdbc.driver}" > </property > <property name ="url" value ="${jdbc.url}" > </property > <property name ="username" value ="${jdbc.username}" > </property > <property name ="password" value ="${jdbc.password}" > </property > </dataSource > </environment > </environments > <mappers > <package name ="com.itheima.dao" > </package > </mappers > </configuration >
编写应用程序 public class App { public static void main (String[] args) throws IOException { SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder (); InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml.bak" ); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); AccountDao accountDao = sqlSession.getMapper(AccountDao.class); Account ac = accountDao.findById(1 ); System.out.println(ac); sqlSession.close(); } }
运行程序 正式整合! 项目中导入整合需要的jar包 </dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 5.2.10.RELEASE</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > **mybatis-spring**</artifactId > <version > 1.3.0</version > </dependency >
创建Spring的主配置类 @Configuration @ComponentScan("com.itheima") public class SpringConfig {}
创建数据源的配置类** public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String password; @Bean public DataSource dataSource () { DruidDataSource ds = new DruidDataSource (); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; } }
创建Mybatis配置类并配置SqlSessionFactory** — 代替Mybatis核心配置文件
public class MybatisConfig {@Bean public SqlSessionFactoryBean sqlSessionFactory (DataSource dataSource) { SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean (); ssfb.setTypeAliasesPackage("com.itheima.domain" ); ssfb.setDataSource(dataSource); return ssfb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer () { MapperScannerConfigurer msc = new MapperScannerConfigurer (); msc.setBasePackage("com.itheima.dao" ); return msc; } }
主配置类中读properties并引入数据源配置类、Mybatis配置类 @Configuration @ComponentScan("com.itheima") **@PropertySource("classpath:jdbc.properties") @Import({JdbcConfig.class,MybatisConfig.class}) **public class SpringConfig {}