全註解不使用配置文件java
首先仍是倒包 在原有的jar包:mysql
需Spring壓縮包中的四個核心JAR包web
beans 、context、core 和expressionspring
下載地址:sql
https://pan.baidu.com/s/1qXLHzAW數據庫
以及日誌jar包express
commons-logging 和log4j網絡
下載地址:app
https://pan.baidu.com/s/1mimTW5idom
再增長一個
spring-aop-5.0.1.RELEASE.jar
增長註解功能的jar包名字是aop有些奇怪(不是annotation ,也不是context)
再增長一個
spring-web-4.2.4.RELEASE.jar
本例用於配置C3P0須要網絡功能,還須要增長數據庫鏈接的jar包
mysql-connector-java-5.1.7-bin.jar
本篇所需jar包打包下載地址:
https://pan.baidu.com/s/1UUKcm82DplON50W10TjX6A
而後,寫一個類代替applicationContext.xml文件
以下:
package cn.itcast.c_all_annotation; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; //代替applicationContext.xml配置文件 @Configuration //至關於<context:component-scan base-package="cn.itcast"> @ComponentScan("cn.itcast") //將DataSourceConfiguration類中的配置引入(分模塊開發) //至關於<import resource="/cn/itcast/property/property_injection.xml"/> @Import(DataSourceConfiguration.class) public class SpringConfiguration { }
分模塊開發方法,引入另外一配置文件 該文件把src下的.properties文件
package cn.itcast.c_all_annotation; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import com.mchange.v2.c3p0.ComboPooledDataSource; //代替applicationContext.xml配置文件 @Configuration /* * <context:property-placeholder location="classpath:db.properties" /> * 讀取類路徑下的db.properties * spel語言 */ @PropertySource("classpath:db.properties") public class DataSourceConfiguration { @Value("${jdbc.jdbcUrl}") private String jdbcUrl; @Value("${jdbc.driverClass}") private String driverClass; @Value("${jdbc.user}") private String user; @Value("${jdbc.password}") private String password; // c3p0鏈接池交給spring容器 // @Bean 將方法的返回值交給spring容器管理.參數就是BeanName @Bean(name = "dataSource") public DataSource getDataSource() throws Exception { // 1 建立鏈接池對象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 2 設置鏈接池參數 dataSource.setJdbcUrl(jdbcUrl); dataSource.setDriverClass(driverClass); dataSource.setUser(user); dataSource.setPassword(password); return dataSource; } @Bean // 該對象配合@PropertySource("classpath:db.properties")註解,完成properties文件讀取 public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() throws Exception { return new PropertySourcesPlaceholderConfigurer(); } }
db.properties 文件內容
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/crm jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=root
測試類以下:
package cn.itcast.c_all_annotation; import java.sql.SQLException; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.domain.Car; import cn.itcast.domain.User; public class Demo { //JUnit不能靜態方法 不能返回值 @Test public void fun1(){ ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); User u = (User) ac.getBean("user"); Car c = (Car) ac.getBean("car"); System.out.println(u); System.out.println(c); } @Test public void fun2() throws SQLException{ ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); DataSource ds = (DataSource) ac.getBean("dataSource"); System.out.println(ds.getConnection()); } }
測試結果截圖
上面測試還順帶測試了一下註解的User和Car類
代碼以下:
package cn.itcast.domain; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; //<bean name="user" class="cn.itcast.domain.User" /> @Component("user") /* //註冊service層對象 @Service @Repository //註冊Dao層對象 @Controller //註冊Web層對象*/ //<bean scope="singleton|prototype" > @Scope("prototype") public class User { @Value("tom") //爲name賦值爲tom private String name; private Integer age; @Resource(name="car") /* * @Autowired 自動注入 有就注入 默認名car開始 * 注意:若是匹配到多個會拋出異常*/ // @Autowired /* * 當自動注入匹配到多個對象時,可使用@Qualifier 指定具體注入哪個(不經常使用) */ @Autowired @Qualifier("car2") private Car car; public User() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } //將賦值註解放到set方法上,可執行方法中判斷邏輯 @Value("18")//爲age賦值 public void setAge(Integer age) { System.out.println("public void setAge(Integer age)!"); this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } //<bean init-method="init" > @PostConstruct public void init() { System.out.println("構造以後初始化方法!"); } //<bean destory-method="destory" > @PreDestroy public void destory() { System.out.println("銷燬以前銷燬方法!"); } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", car=" + car + "]"; } }
Car類
package cn.itcast.domain; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Car { @Value("哈佛H6") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car [name=" + name + "]"; } }
以上實現了全註解的Spring設計,並整合了c3p0,且不須要修改DataSourceConfiguration 類中的內容,直接修改.properties文件便可
全註解方式雖然替換掉了XML配置文件 但操做相對繁瑣 不建議使用
若是能夠直接在類中修改代碼就幾行搞定,若是使用xml配置也就幾行就能夠了,比較一下哪一個簡便
package cn.itcast.b_datasource; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mchange.v2.c3p0.ComboPooledDataSource; public class Demo { @Test //手動建立C3p0鏈接池 public void fun1() throws Exception{ //1 建立鏈接池對象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); //2 設置鏈接池參數 dataSource.setJdbcUrl("jdbc:mysql:///crm"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setMinPoolSize(1); System.out.println(dataSource.getConnection()); }
下邊是xml方法
@Test //從Spring容器中得到C3p0鏈接池 public void fun2() throws Exception{ //1 建立spring容器 ApplicationContext ac = new ClassPathXmlApplicationContext( "/cn/itcast/b_datasource/dataSource.xml"); //2 得到鏈接池 DataSource ds = (DataSource) ac.getBean("dataSource"); //3 測試 System.out.println(ds.getConnection()); }
配置文件配置以下:
<?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配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- ${jdbc.jdbcUrl} => 引用db.properties文件中jdbc.jdbcUrl對應的值 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property> <property name="driverClass" value="${jdbc.driverClass}" ></property> <property name="user" value="${jdbc.user}" ></property> <property name="password" value="${jdbc.password}" ></property> <property name="minPoolSize" value="1" ></property> </bean> </beans>
讀取的.properties文件上面已經給過
xml配置文件沒有提示解決
window -->preferences-->搜索 xml catalog -->add-->在key中輸入約束網址http://www.springframework.org/schema/beans/spring-beans.xsd
-->在location中點擊按鈕 file system-->找到spring framework 包-->schema-->beans-->最後的那個版本高的-->key type下拉菜單選schema location