IOC 操作 Bean 管理(外部属性文件)
1、直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖jar包
JDBC中包含官网下载
src下/新建bean6.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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 直接配置数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Drive"></property><!-- 为8.0之后 前为 com.mysql.jdbc.Drive --> <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> </beans>
2、引入外部的属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,数据库信息
src下/新建 jdbc.properties
prop.driverClass=com.mysql.cj.jdbc.Drive prop.url=jdbc:mysql://localhost:3306/userDb prop.username=root prop.password=root
(2)把外部properties属性文件引入到spring配置文件中
- 引入context名称空间
- 在spring配置文件使用标签引入外部属性文件
bean6.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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 直接配置数据库连接池 --> <!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">--> <!-- <property name="driverClassName" value="com.mysql.cj.jdbc.Drive"></property><!– 为8.0之后 前为 com.mysql.jdbc.Drive –>--> <!-- <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>--> <!-- <property name="username" value="root"></property>--> <!-- <property name="password" value="root"></property>--> <!-- </bean>--> <!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!-- 直接配置数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${prop.driverClass}"></property> <property name="url" value="${prop.url}"></property> <property name="username" value="${prop.username}"></property> <property name="password" value="${prop.password}"></property> </bean> </beans>
IOC 操作 Bean 管理(基于注解方法)
1、什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化xml配置
2、Spring针对Bean管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository
- 上面的四个注解功能是一样的,都可以用来创建bean实例
3、基于注解方式实现对象创建
第一步 引入依赖
spring-aop-5.3.10.jar
第二步 开启组件扫描
新建一个模块(项目)spring5_demo3/src下/新建bean1.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:p="http://www.springframework.org/schema/p" 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"> <!-- 开启组件扫描 1 如果扫描多个包,多个包使用逗号隔开 2 扫描包上层目录 --> <context:component-scan base-package="com.atguigu"></context:component-scan> </beans>
第三步 创建类,在类上面添加创建对象注解
spring5/新建service/新建UserService
package com.atguigu.spring5.service; import org.springframework.stereotype.Component; //在注解里面value属性值可以省略不写 //默认值是类名称,首字母小写 //UserService -- userService @Component(value = "userService")//<bean id="userService" class",,"/> public class UserService { public void add(){ System.out.println("service add........."); } }
TestSpring5Demo1
package com.atguigu.spring5.testdemo; import com.atguigu.spring5.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring5Demo1 { @Test public void testService(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); UserService uservice = context.getBean("userService", UserService.class); System.out.println(uservice); uservice.add(); } }
4、开启组件扫描细节配置
bean1.xml
<!-- 实例1 use-default-filters="false" 表示现在不使用默认filter,自己配置filter context:include-filter ,设置扫描哪些内容 --> <context:component-scan base-package="com.atguigu" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 实例2 下面配置扫描所有内容 context:exclude-filter ,设置哪些内容 不 进行扫描 --> <context:component-scan base-package="com.atguigu"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
5、基于注解方式实现属性注入
(1)@Autowired:根据属性类型进行自动装配
第一步 把service和dao对象创建,在service和dao类添加创建对象注解
spring5/新建dao/新建UserDao接口
package com.atguigu.spring5.dao; public interface UserDao { public void add(); }
dao/新建UserDaoImpl
package com.atguigu.spring5.dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao{ @Override public void add() { System.out.println("dao add....."); } }
第二部在service注入dao对象,在service类中添加dao类型属性,在属性上面使用注解
UserService
package com.atguigu.spring5.service; import com.atguigu.spring5.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里面value属性值可以省略不写 //默认值是类名称,首字母小写 //UserService -- userService //@Component(value = "userService")//<bean id="userService" class",,"/> //@Controller //@Repository @Service public class UserService { //定义dao类型属性 //不需要添加set方法 //添加注入属性注解 @Autowired private UserDao userDao; public void add(){ System.out.println("service add........."); userDao.add(); } }
执行TestSpring5Demo1
com.atguigu.spring5.service.UserService@73e22a3d service add......... dao add..... Process finished with exit code 0
(2)@Qualifier:根据属性名称进行自动注入
这个@Qualifier注解的使用,和上面@Autowired一起使用
package com.atguigu.spring5.service; import com.atguigu.spring5.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里面value属性值可以省略不写 //默认值是类名称,首字母小写 //UserService -- userService //@Component(value = "userService")//<bean id="userService" class",,"/> //@Controller //@Repository @Service public class UserService { //定义dao类型属性 //不需要添加set方法 //添加注入属性注解 @Autowired//根据类型进行注入 @Qualifier(value = "userDaoImpl")//根据名称进行注入,当UserDao有多个实现类时,可以精确找到 private UserDao userDao; public void add(){ System.out.println("service add........."); userDao.add(); } }
(3)@Resource:可以根据类型注入,可以根据名称注入
jdk11之后开始移除了,spring官方不建议用因为在javax.annotation.Resource
(4)@Value:注入普通类型属性
UserService
package com.atguigu.spring5.service; import com.atguigu.spring5.dao.UserDao; 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.ImportResource; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里面value属性值可以省略不写 //默认值是类名称,首字母小写 //UserService -- userService //@Component(value = "userService")//<bean id="userService" class",,"/> //@Controller //@Repository @Service public class UserService { @Value(value = "abc") private String name; //定义dao类型属性 //不需要添加set方法 //添加注入属性注解 @Autowired//根据类型进行注入 @Qualifier(value = "userDaoImpl")//根据名称进行注入,当UserDao有多个实现类时,可以精确找到 private UserDao userDao; public void add(){ System.out.println("service add........."+name); userDao.add(); } }
6、完全注解开发
(1)创建配置类,替代xml配置文件
spring5下/新建config包/新建SpringConfig
package com.atguigu.spring5.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration //作为配置类,替代xml配置文件 @ComponentScan(basePackages = {"com.atguigu"}) public class SpringConfig { }
(2)编写测试类
TestSpring5Demo1
@Test public void testService2(){ //加载配置类 ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class); UserService uservice = context.getBean("userService", UserService.class); System.out.println(uservice); uservice.add(); }
- 回到顶部↩︎