1.使用spring需要加的jar包
commons-logging
spring-core
spring-beans
spring-expression
spring-context
图中可以看出spring-context是最底下的包 只需要引入
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.25.RELEASE</version> </dependency>
2.引入xml文件的方式
BeanFactory bf = new ClassPathXmlApplicationContext("Spring.xml"); BeanFactory bf = new FileSystemXmlApplicationContext("D:Spring.xml"); BeanFactory bf = new ClassPathXmlApplicationContext("classpath*:Spring*");//引入以Spring开头的文件
3.Spring.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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-4.1.xsd "> <!-- 启用注解,可以不加,会自动开启--> <context:annotation-config></context:annotation-config> <!--告诉他去找哪些路径下的类--> <context:component-scan base-package="com.blb"></context:component-scan> <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:6666/haha"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--property可用p代替--> <bean id="c3p02" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost:6666/haha" p:user="root" p:password="123456" > </bean> <!-- 注入方式2构造器注入,index标识第几个参数,ref是参数类型,value传入的值--> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <constructor-arg index="0" ref="c3p0"></constructor-arg> </bean> <!-- 注入前提属性要有set方法--> <bean id="pink" class="com.blb.dto3.Pink"> <property name="pid" value="1"></property> <property name="color" value="grenn"></property> </bean> </beans>
4.加注解的方式加入ioc容器
@Service 用来标注业务层 只要标注了改注解的类 会自动加入到ioc容器
@Controller 控制层
@Repository 持久层
@Component 其他
不强制对应
自己写的类可以加注释, jar包里的类不能用,只能用原始的方式加到ioc容器
@Autowired 就是bytype 加在属性上
@Resource 就是byname 现根据名字找。找不到再根据类型找
controller:
package com.blb.controller; import com.blb.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class UserController { @Autowired private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
service:
package com.blb.service; import com.blb.dao.UserDao; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserService { @Resource private UserDao userdao; public UserDao getDao() { return userdao; } public void setDao(UserDao userdao) { this.userdao = userdao; } }
dao:
package com.blb.dao; import com.blb.util.C3p0Util; import org.apache.commons.dbutils.QueryRunner; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class UserDao { @Autowired private QueryRunner runner; public QueryRunner getRunner() { return runner; } public void setRunner(QueryRunner runner) { this.runner = runner; } }
5.用配置文件Bean的方式加入ioc容器
<?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="com.blb.dao.UserDao" autowire="byType"> </bean> <bean id="userService" class="com.blb.service.UserService" autowire="byName"> </bean> <bean id="userController" class="com.blb.controller.UserController" autowire="byType"> </bean> </beans>
6.测试运行
@Test public void buildIOC(){ BeanFactory bf = new ClassPathXmlApplicationContext("Spring.xml"); UserController controller=(UserController)bf.getBean("userController"); QueryRunner runner = controller.getUserService().getDao().getRunner(); try { List<User> query = runner.query("select * from t_user",new BeanListHandler<User>(User.class)); for(User user:query){ System.out.println(user); } } catch (SQLException e) { e.printStackTrace(); } } }