Spring是一款轻量级开源框架
IoC(Inverse Of Control:反转控制)通过配置的方式,将对象的管理控制权交给spring容器,spring得到具体的包路径,类路径通过反射的方式,创建对象。
AOP(Aspect Oriented Programming:面向切面编程),运行期通过动态代理方式,在方法的前后加入相应的通知(日志,权限等)
Bean标签基本属性配置:
id:Bean实例在Spring容器中的唯一标识
class:Bean的全限定名称
scope:指对象的作用范围
init-method:指定类中的初始化方法名称
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有多个时,则此方法会报错