Spring进阶篇

spring中如何配置c3p0连接池

  • 导包

4+2 包+c3p0包(spring的依赖包中)+数据库驱动.jar

  • 准备db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///crm
jdbc.user=root
jdbc.password=root
  • spring配置中加载db.properties文件信息
<context:property-placeholder location="classpath:db.properties" />

  • c3p0配置中引用读取到的db.properties

使用${key}引用读到的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
	   
	   <!-- 读取外部properties文件
	   			1.引入Context约束
	   			2.使用property-placeholder元素完成properties文件引入
	    -->
	    <context:property-placeholder location="classpath:db.properties" />
	   <!-- 使用${key}引用读取到的properties -->
	   <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
	   		<property name="driverClass" value="${jdbc.driverClass}" ></property>
	   		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
	   		<property name="user" value="${jdbc.user}" ></property>
	   		<property name="password" value="${jdbc.password}" ></property>
	   </bean>
	   <!-- 引入其他xml配置 -->
	   <import resource="cn/itcast/c_annotation/annotation.xml"/>
</beans>
	   

Spel表达式如何使用

介绍:SPEL Spring Expression Language 的缩写. spring表达式语言.
可以在spring配置中使用.用于方便的引用其他Bean的属性

在这里插入图片描述

spring中注册Bean注解

  • 导入所依赖的包

4+2包+spring-aop包
在这里插入图片描述
在这里插入图片描述

  • 开启注解注册Bean开关
 <context:component-scan base-package="cn.itcast"></context:component-scan>
  1. 基本注解

类名上

@component 把该类注册成bean,
@Controller 用于标识web层,
@Service 用于标识service层,
@Repository 用于标识dao层,
4个注解都是一样的功能,用于表示不同的层
@scope(“值”):singleton单例(默认),prototype多例,action配置时必须是多例的

方法上

@PostConstruct 初始化方法
@PreDestroy销毁方法

  1. 注入注解

注解没有集合数组类型注入

值类型注入

属性上@value(“值”),通过反射直接对成员变量赋值(不走set方法)
set方法上@value(“值”),调用set方法赋值

对象类型注入,也可以加在set方法上,与值类型注入区别相同

对象属性上@Autowired,自动装配->自动从容器查找相同类型的对象,并注入到属性中。注意:如果找到多个类型匹配的对象将抛出异常

@Autowired @Qualifier(“car2”),多个类型相同的对象时,可以通过@Qualifier具体指定注入哪一个

@Resource(name=“car1”) 指定匹配对象

在xml中引入其他xml配置

在这里插入图片描述

spring整合junit测试

4+2包+spring-aop包+spring-text包+junit4包
在这里插入图片描述

spring完全使用注解开发,目前比较火的springboot底层的一些自动配置机制,如果你去翻代码的话,还是依赖于这样spring原注解。

概述:自建一个类作为配置文件,相当于applicationContext,类中使用注解完成配置文件的配置。

@Configuration 标志该类是spring中的xml配置文件	
@ComponentScan("cn.itcast") 开启bean注解开关
@Import({类.class,类.class}) 引入其他配置文件,可以引入多个

@PropertySource
1.在类上注解,要加载的文件@PropertySource("classpath:db.properties")
2.配合第一步,将properties文件读取

@Bean
	public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	3.声明对应属性,使用@value("${jdbc.driverClass}")注解为声明的属性注入值
	@Value("${jdbc.driverClass}")
	private String driverClass;
	@Value("${jdbc.jdbcUrl}")
	private String jdbcUrl;
	@Value("${jdbc.user}")
	private String user;
	@Value("${jdbc.password}")
	private String password;


	@Bean(name="dataSource")
	将注解所在的方法返回值交给容器管理,该对象再容器中的bean的name为dataSource

猜你喜欢

转载自blog.csdn.net/weixin_41349389/article/details/82848849