Spring是一款輕量級開源框架java
IoC(Inverse Of Control:反轉控制)經過配置的方式,將對象的管理控制權交給spring容器,spring獲得具體的包路徑,類路徑經過反射的方式,建立對象。mysql
AOP(Aspect Oriented Programming:面向切面編程),運行期經過動態代理方式,在方法的先後加入相應的通知(日誌,權限等)spring
Bean標籤基本屬性配置:sql
id:Bean實例在Spring容器中的惟一標識數據庫
class:Bean的全限定名稱編程
scope:指對象的做用範圍api
init-method:指定類中的初始化方法名稱app
destroy-method:指定類中銷燬方法名稱框架
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.3.RELEASE</version> </dependency> </dependencies>
public interface UserDao { public void save(); } public class UserDaoImpl implements UserDao { public void save() { System.out.println("save running...."); } }
在類路徑下(resources)建立applicationContext.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"> </beans>
<?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="userDao" class="cn.guixinchn.dao.impl.UserDaoImpl"></bean> </beans>
public class test { @Test public void test1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao"); userDao.save(); } }
依賴注入(Dependency Injection):它是 Spring 框架核心 IOC 的具體實現
①set方法注入
建立UserServer接口和UserServerImpl實現類
經過Spring配置文件的方式
public interface UserServer { public void save(); } public class UserServerImpl implements UserServer { public void save() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao"); userDao.save(); } }
<bean id="userServer" class="cn.guixinchn.server.impl.UserServerImpl"></bean>
經過配置set方式
public class UserServerImpl implements UserServer { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); } }
<bean id="userServer" class="cn.guixinchn.server.impl.UserServerImpl"> <property name="userDao" ref="userDao"></property> </bean>
②構造方法注入
public class UserServerImpl implements UserServer { private UserDao userDao; public UserServerImpl(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); } }
<bean id="userServer" class="cn.guixinchn.server.impl.UserServerImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
如上
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"> <property name="name" value="張三"></property> <property name="age" value="18"></property> </bean>
<property name="strList"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property>
<property name="userMap"> <map> <entry key="a" value-ref="aaa"/> <entry key="b" value-ref="bbb"/> </map> </property>
<property name="properties"> <props> <prop key="a">aaa</prop> <prop key="b">bbb</prop> <prop key="c">ccc</prop> </props> </property>
能夠把配置文件分出來,在Spring主配置文件經過import標籤進行加載
<import resource="applicationContext-xxx.xml"/>
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency>
在resources目錄下新建一個jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123456
applicationContext.xml加載jdbc.properties配置文件得到鏈接信息。
首先,須要引入context命名空間和約束路徑:
<!--Spring容器加載properties文件--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
@Test public void test3() throws SQLException { //獲取配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //拿到數據源 DataSource dataSource = (DataSource) applicationContext.getBean("dataSource"); //得到鏈接對象 Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); }
applicationContext:接口類型,表明應用上下文,能夠經過其實例得到 Spring 容器中的 Bean 對象
當參數的數據類型是字符串時,表示根據Bean的id從容器中得到Bean實例,返回是Object,須要強轉。
當參數的數據類型是Class類型時,表示根據類型從容器中匹配Bean實例,當容器中相同類型的Bean有多個時,則此方法會報錯