这就是使用简单的配置
logging这个也是需要在最出的时候就配置
Spring框架创建配置
控制反转IOC(把控制权交给spring控制)和面向切面编程APO(呢过滤器很像)
1首先导入主要的包
2在资源文件中创建一个applicartionContext.xml的配置文件
并且配置设置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <!-- 配置最简单的beans --> <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"> </beans>
3. 通过<bean id =”” class=””>交给spring管理
4. 获得spring中的对象
1 三种方式,工厂
ClassPathResource resource = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
Person person = factory.getBean("person",Person.class);
2. 上下文
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person1 = context.getBean("person",Person.class);
3. @autowired方式
在定一的时候的打上注解就可以了
Spring测试和junit测试
Spring测试也是建立在junit测试的上面,只不过junit测试每次都要创建,spring测试不需要每次都使用spring,看图把
需要导入相应的包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cun.jiedada.SpringTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //加载junt4测试 @RunWith(SpringJUnit4ClassRunner.class) //加载配置文件 @ContextConfiguration("classpath:applicationContext.xml") public class SpringTest1 { //自动注入 /*@Autowired public Person person;*/ @Autowired public Phone phone; @Test public void f(){ //System.out.println(person); System.out.println(phone); } }
属性注入
分为set注入和构造方法注入
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!-- <bean id="phone" class="cun.jiedada.SpringTest.Phone"> 构造器注入 <constructor-arg index="0" value="inter 5"></constructor-arg> setter注入通过bean属性找到setter方法添加值 <property name="sim" value="中国电信"></property> <property name="cd" value="16G"></property> </bean> -->
配置连接池
把连接池的东西配置到我们的spring管理
也需要导入包,还需要解决硬编码问题
需要使用context来配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<bean id="data" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
三层架构
通过@autowired
只需要这样一个注解,只要是applicetionContext.xml中的默认配置就全部配置完成所以我们只需要一个注解,但是怎么通过<bean>连接不同类呢?我们可以通过ref属性,把每个<bean连接起来>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <!-- 配置最简单的beans --> <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 --> <!-- <bean id="person" class="cun.jiedada.SpringTest.Person" ></bean> <bean id="date" class="java.util.Date"></bean> <bean id="list" class="java.util.ArrayList"></bean> --> <!-- <bean id="phone" class="cun.jiedada.SpringTest.Phone"> 构造器注入 <constructor-arg index="0" value="inter 5"></constructor-arg> setter注入通过bean属性找到setter方法添加值 <property name="sim" value="中国电信"></property> <property name="cd" value="16G"></property> </bean> --> <!-- 是我们能够获得properties文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 连接池,dao,service,的相互依赖关系 --> <bean id="data" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="dao" class="cn.jiedada.project.dao.impl.ManImpl"> <property name="dbs" ref="data"></property> </bean> <bean id="service" class="cn.jiedada.project.service.impl.ManServiceImpl"> <property name="dao" ref="dao"></property> </bean> </beans>
package cn.jiedada.project.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import cn.jiedada.project.dao.IManDao; import cn.jiedada.project.domain.Man; public class ManImpl implements IManDao{ private BasicDataSource dbs; public ManImpl() { } public void setDbs(BasicDataSource dbs) { this.dbs = dbs; } @Override public void add(Man man) { try { Connection conn = dbs.getConnection(); String sql="insert into man(name) values(?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, man.getName()); ps.execute(); ps.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
一定要提供规范的Javabean规范(有无参构造,提供getset方法,字段私有话),其他代码根本不用变,如果使用的话打上@autowired就可以了