spring学习笔记2
IOC操作Bean管理(xml自动装配)
1、什么是自动装配
1)根据指定的装配规则(属性名称或者属性类型),spring自动将匹配的属性值进行注入
2、演示自动装配
1)byName根据属性名称注入
<?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: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标签属性autowire,配置自动装配 autowire属性常用的两个值: byName根据属性名称注入,注入值bean的id值和类属性名称一样 byType根据属性类型注入 --> <bean id="emp" class="com.autowire.Emp" autowire="byName"> <!-- <property name="dept" ref="dept"></property>--> </bean> <!----> <bean id="dept" class="com.autowire.Dept"></bean> </beans>
package com.autowire; public class Emp { private Dept dept; public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Dept{" + "dept=" + dept + '}'; } public void test(){ System.out.println(dept); } }
package com.autowire; public class Dept { @Override public String toString() { return "Dept{}"; } private Dept dept; }
2)byType根据属性类型注入
IOC操作Bean管理(外部属性文件)
1、直接配置数据库信息
1)配置德鲁伊连接池
2)引入德鲁伊连接池jar包
2、引入外部属性文件配置数据库连接池
1)创建外部属性文件 ,properties格式文件,数据库信息
2)把外部properties属性文件引入到spring配置文件中
引入context名称空间
在spring
<?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: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.jdbc.Driverclass"></property>--> <!-- <property name="url" value="jdbc:mysql://localhost:3306"></property>--> <!-- <property name="username" value="root"></property>--> <!-- <property name="password" value="mxy1994"></property>--> <!-- </bean>--> <context:property-placeholder location="classpath:idbc.properties"/> <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.password1>"></property> </bean> </beans>
IOC操作Bean管理(基于注解方式)
1、什么是注解
1)注解是代码中的特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...)
2) 使用注解:注解作用在类上面,方法上面,属性上面
3)目的:简化xml配置
2、Spring针对Bean管理中创建对象提供注解
1) @Component
2) @Service
3) @Controller
4) @Respository
上面的注解功能是一样的,都可以用来创建bean实例
package com.spring.sercice; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里value属性值可以省略不写,不写的话默认是类名称首字母小写 //@Component(value = "userService") // 这种方法等于<bean id="userService" class=""/> //@Service //@Controller @Repository public class UserService { public void add(){ System.out.println("UserService...."); } }
<?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: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"> <!--开启组件扫描 1)扫描多个包,多个包之间用逗号隔开 2)写扫描包的上层目录 --> <context:component-scan base-package="com.spring"></context:component-scan> </beans>
3、基于注解方式实现对象的创建
1)引入aop依赖
2)开启组件扫描
4、扫描过滤
<?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: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"> <!--开启组件扫描 1)扫描多个包,多个包之间用逗号隔开 2)写扫描包的上层目录 --> <context:component-scan base-package="com.spring"></context:component-scan> <!--示例1 use-default-filters="false"表示现在不使用默认filter,自己配置filter context:include-filter,设置扫描哪些内容--> <context:component-scan base-package="com.spring" 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.spring"> <context:exclude-filter type="annotation" expression="org. springframework. stereotype.Controller"/> </context:component-scan> </beans>
5、基于注解方式实现属性注入
1)@Autowired: 根据属性类型进行自动装配
a)创建service和dao对象,在其类上添加注解
b)在service里注入dao对象,添加dao类型属性,在属性上面使用注解
package com.spring.dao; public interface UserDao { public void add(); } package com.spring.dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao{ @Override public void add(){ System.out.println("dao add...."); } }
package com.spring.sercice; import com.spring.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里value属性值可以省略不写,不写的话默认是类名称首字母小写 //@Component(value = "userService") // 这种方法等于<bean id="userService" class=""/> //@Service //@Controller @Service public class UserService { //定义dao属性,不需要添加set方法 @Autowired//根据类型进行注入 private UserDao userDao; public void add(){ System.out.println("UserService...."); userDao.add(); } }
2)@Qualifier:根据属性名称进行注入
和autowired一起使用
package com.spring.dao; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Repository; @Repository(value="userDaoImpl1") public class UserDaoImpl implements UserDao{ @Override public void add(){ System.out.println("dao add...."); } }
package com.spring.sercice; import com.spring.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.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里value属性值可以省略不写,不写的话默认是类名称首字母小写 //@Component(value = "userService") // 这种方法等于<bean id="userService" class=""/> //@Service //@Controller @Service public class UserService { //定义dao属性,不需要添加set方法 @Autowired//根据类型进行注入 @Qualifier(value="userDaoImpl1")//根据名称进行注入 private UserDao userDao; public void add(){ System.out.println("UserService...."); userDao.add(); } }
3) @Resource:可以根据类型,可以根据名称注入
4)@Value:注入普通类型属性
package com.spring.sercice; import com.spring.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.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; //在注解里value属性值可以省略不写,不写的话默认是类名称首字母小写 //@Component(value = "userService") // 这种方法等于<bean id="userService" class=""/> //@Service //@Controller @Service public class UserService { //定义dao属性,不需要添加set方法 @Autowired//根据类型进行注入 //@Qualifier(value="userDaoImpl1")//根据名称进行注入 private UserDao userDao; @Value(value = "abc") private String name; public void add(){ System.out.println("UserService...."+name); userDao.add(); } }
IOC操作Bean管理(完全注解开发)
1、创建配置类,替代xml配置文件
package com.spring.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration //把当前类作为配置类,替代xml文件 @ComponentScan(basePackages = {"com.spring"}) public class SpringConfig { }