建设公司网站需要多少钱,p2p网站开发公司,网站运营方法,专做衬衫的网站文章目录1. IOC容器概念2. IOC底层原理3. IOC#xff08;接口#xff09;4. IOC操作Bean管理#xff08;概念#xff09;5. IOC操作Bean管理#xff08;基于xml方式#xff09;5.1 基于xml创建对象5.2 基于xml方式注入属性5.2.1 DI#xff1a;依赖注入#xff0c;就是注…
文章目录1. IOC容器概念2. IOC底层原理3. IOC接口4. IOC操作Bean管理概念5. IOC操作Bean管理基于xml方式5.1 基于xml创建对象5.2 基于xml方式注入属性5.2.1 DI依赖注入就是注入属性5.2.2 xml注入其他类型属性5.2.3 注入属性-外部bean5.2.4 注入属性-内部bean很多人喜欢用外部bean结构明显5.2.5 注入属性-级联赋值5.2.6 xml注入集合属性数组、List、Map、Set6. IOC操作Bean管理FactoryBean7、IOC操作Bean管理bean作用域8. IOCBean管理bean生命周期9. IOC操作Bean管理xml自动装配10. IOC操作Bean管理外部属性文件11. IOC操作Bean管理基于注解方式简化xml配置12. 基于注解方式实现属性注入13. 完全注解开发1. IOC容器概念
控制反转把对象创建和对象之间的调用过程交给Spring进行管理。使用IOC目的为了耦合度降低
2. IOC底层原理
XML解析、工厂设计模式、反射IOC过程 第一步xml配置文件配置创建的对象 bean iddao classcom.zsh.UserDao/bean第二步有service类和dao类创建工厂类 class UserFactory{public static UserDao getDao(){//1.xml解析 String classValue class属性值; //2.通过反射创建对象Class clazz Class.forName(classValue);return (UserDao)clazz.getDeclaredConstructor().newInstance();}
}3. IOC接口
IOC思想基于IOC容器完成IOC容器底层就是对象工厂Spring提供IOC容器实现方式两个接口 BeanFactoryIOC容器基本实现是Spring内部的使用接口不提供开发人员进行使用。 加载配置获取文件时候不会创建对象在获取对象使用才去创建对象。ApplocationContextBeanFactory接口的子接口提供更多更强大的功能一般由开发人员进行使用。 加载配置文件时候就会把在配置文件中的对象进行创建。 ApplicationContext接口有实现类
4. IOC操作Bean管理概念
什么是Bean管理 Bean管理有两方面 Spring创建对象Spring注入属性 Bean管理操作有两种方式 基于xml配置文件方式实现基于注解方式实现
5. IOC操作Bean管理基于xml方式
5.1 基于xml创建对象
!-- 配置User对象 --
bean iduser classcom.spring.User/bean在spring配置文件中使用bean标签标签里面添加对应属性就可以实现对象创建在bean标签有很多属性介绍常用的属性 id属性唯一标识class属性类全路径包类路径 创建对象时候默认也是执行无参数构造方法完成对象创建。
5.2 基于xml方式注入属性
5.2.1 DI依赖注入就是注入属性
第一种注入方式set注入方式 创建类定义属性和对应的set方法在spring配置文件中配置对象创建配置属性注入。 bean idbook classzsh.spring.Book!--2.set方法注入属性--!--name类里面属性名称value向属性注入的值--property namebname value天龙八部/propertyproperty namebauthor value金庸/property
/bean第二种注入方式有参数构造方式 创建类定义属性创建属性对应有参构造方法。在spring配置文件中配置。
xml
!--3.有参构造注入属性--
bean idorders classzsh.spring.Ordersconstructor-arg nameoname valueabc/constructor-argconstructor-arg nameaddress valueChina/constructor-arg
/beanp名称空间注入了解 使用p名称空间注入可以简化基于xml配置方式。 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd进行属性注入在bean标签里面进行操作。
5.2.2 xml注入其他类型属性
字面量 null值 property nameaddressnull/null
/property属性值包含特殊符号 !--属性值包含特殊符号1.把《》进行转义2.把带特殊符号内容写到CDATA中--
property namebnamevalue![CDATA[《天龙八部》]]/value
/property5.2.3 注入属性-外部bean
创建两个类service类和dao类在service调用到里面的方法public interface UserDao {public abstract void update();}
public class UserDaoImpl implements UserDao {Overridepublic void update() {System.out.println(dao update....);}
}
public class UserService {private UserDao userDao;public void setUserDao(UserDao userDao){this.userDao userDao;}public void add(){System.out.println(service add .....);userDao.update();}
}在xml中配置!-- 1 service和dao对象创建 --
bean iduserService classzsh.service.UserService!--name属性类里面的属性名称ref属性创建userDaoImpl对象bean标签--property nameuserDao refuserDaoImpl/property
/bean
bean iduserDaoImpl classzsh.dao.Impl.UserDaoImpl/bean5.2.4 注入属性-内部bean很多人喜欢用外部bean结构明显
一对多关系部门和员工一个部门多个员工一个员工属于某一个部门 在实体类之间表示一对多关系员工表示所属部门使用对象类型属性进行表示//部门类
public class Dept {private String dname;public void setDname(String dname) {this.dname dname;}
}
//员工类
public class Emp {private String ename;private String gender;//员工属于某一个部门private Dept dept;public void setDept(Dept dept) {this.dept dept;}public void setEname(String ename) {this.ename ename;}public void setGender(String gender) {this.gender gender;}
}在spring配置中进行配置。!-- 内部bean --
bean idemp classzsh.bean.Empproperty nameename valuelucky/propertyproperty namegender value女/propertyproperty namedeptbean iddept classzsh.bean.Deptproperty namedname value安保部/property/bean/property
/bean5.2.5 注入属性-级联赋值
方式一同外部bean注入方式二xml配置如下需要在emp表给dept设置getter!-- 级联赋值 --
bean idemp classzsh.bean.Empproperty nameename valuelucky/propertyproperty namegender value女/propertyproperty namedept refdept/propertyproperty namedept.dname value技术部/property
/bean
bean iddept classzsh.bean.Deptproperty namedname value安保部/property
/bean5.2.6 xml注入集合属性数组、List、Map、Set
创建类、定义数组、List、Map、Set类型属性生成对应set方法。public class Stu {private String[] courses;private ListString list;private MapString,String maps;private SetString sets;public void setCourses(String[] courses){this.courses courses;}public void setList(ListString list){this.list list;}public void setMaps(MapString,String maps){this.maps maps;}public void setSets(SetString sets){this.sets sets;}
}在Spring配置文件进行配置。!-- 集合类型属性注入 --
bean idstu classzsh.shuzu.Stu!-- 数组类型属性注入 --property namecoursesarrayvaluejava/value/array/property!-- list类型属性注入,值是对象类型 --property namelistlistref beancourse1/refref beancourse2/ref/list/property!-- Map类型属性注入 --property namemapsmapentry keyJAVA valuejava/entry/map/property!-- Set类型属性注入 --property namesetssetvalueMySql/value/set/property
/bean
!--创建多个Course对象--
bean idcourse1 classzsh.shuzu.Courseproperty namecname valueSpring5框架/property
/bean
bean idcourse2 classzsh.shuzu.Courseproperty namecname valueMybatis框架/property
/bean把集合注入部分提取出来。 在spring配置文件中引入名称空间util beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd使用标签完成list集合注入提取 util:list idbookListref beancourse1/ref
/util:list
!-- list类型属性注入,值是对象 --
property namecourseList refbookList/property注意面试中会问DI 和 IOC区别 DI 是 IOC中一种具体实现就表示依赖注入注入属性注入属性需要在创建对象的基础之上。
6. IOC操作Bean管理FactoryBean
Spring有两种类型bean一种普通bean另一种工厂beanFactoryBean
普通bean在配置文件中定义bean类型就是返回类型上面的配置bean就是普通bean工厂bean在配置文件定义bean类型可以和返回值类型不一样 第一步创建类让这个类作为工厂bean实现接口FactoryBean第二步实现接口里面的方法在实现的方法中定义返回的bean类型public class MyBean implements FactoryBeanCourse {//定义返回beanOverridepublic Course getObject() throws Exception {Course course new Course();course.setCname(abc);return course;}Overridepublic Class? getObjectType() {return null;}
}7、IOC操作Bean管理bean作用域
在Spring里面设置创建bean实例默认为单实例对象如何设置单实例还是多实例 在spring配置文件bean标签里面有属性scope用于设置scope属性值 默认值sigleton表示单实例对象 prototype表示多实例对象。 注意 sigleton和prototype区别 第一sigleton单实例prototype多实例 第二设置scope是sigleton时候加载spring配置文件时候就会创建单实例对象。 第三设置scope是prototype时候不是在加载spring配置文件时候创建对象在调用getBean方法时候创建多实例对象。
8. IOCBean管理bean生命周期
生命周期从对象创建到对象销毁的过程bean生命周期五步配置后置处理器七步 通过构造器创建bean实例无参数构造为bean的属性设置值和对其它bean引用调用set方法调用bean的初始化的方法需要进行配置初始化的方法bean可以使用了对象获取到了当容器关闭时候调用bean的销毁的方法需要进行配置销毁的方法
bean idxxx classxxx init-methodinitMethod destroy-methoddestroyMethodbean的后置处理器bean生命周期有七步创建类实现BeanPostProcessor并在xml配置成bean 把bean实例传递bean后置处理器的方法调用类的before调用bean的初始化的方法需要进行配置初始化的方法把bean实例传递bean后置处理器的方法调用类的after
9. IOC操作Bean管理xml自动装配
什么是自动装配 根据指定装配规则属性名称或者属性类型Spring自动将匹配的属性值进行注入。spring文件中配置Emp中有Dept属性
!--实现自动装配autowire属性常用两个值byName根据属性名称注入 注入bean的id值和类的属性名称一样byType根据属性类型注入
--
bean idemp classautowire.Emp autowirebyName/bean
bean iddept classzsh.autowire.Dept/bean10. IOC操作Bean管理外部属性文件
直接配置数据库信息不展示了引入外部属性文件配置数据库连接池德鲁伊连接池
!-- 引入外部属性文件 --
context:property-placeholder locationclasspath:jdbc.properties/
!-- 配置连接池 --
bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driverClassName}/propertyproperty nameurl value${jdbc.url}/propertyproperty nameusername value${jdbc.username}/propertyproperty namepassword value${jdbc.password}/property
/bean11. IOC操作Bean管理基于注解方式简化xml配置
Spring针对Bean管理中创建对象提供注解 Component(value“”) Service(value“”) Controller(value“”) Repository(value“userDaoImp”) 上面四个注解功能是一样的都可以用来创建bean实例基于注解方式实现对象创建 第一步引入aop依赖jar包第二步开启组件扫描!--1 如果扫描多个包多个包使用逗号隔开 2.扫描包上层目录--context:component-scan base-package/第三步创建类在类上面添加创建对象注解 注意开启组件扫描细节配置!--示例一use-default-filtersfalse 表示现在不使用默认filter自己配置filtercontext:include-filter 表示扫描包下Controller注解
--
context:component-scan base-packagecom use-default-filtersfalsecontext:include-filter typeannotation expressionorg.springframework.stereotype.Controller/
/context:component-scan!-- 设置不去扫描哪些内容 --
context:component-scan base-packagecomcontext:exclude-filter typeannotation expressionorg.springframework.stereotype.Controller/
/context:component-scan12. 基于注解方式实现属性注入
Spring提供的注解 AutoWired根据属性类型进行自动装配 Qualifier(value“userDaoImp”)根据属性名称进行自动装配和Autowired一起使用 Reaource(name“userDaoImp”)可以根据类型注入也可以根据名称注入 注意此注解的包是javax中的不是spring中的 Value注入普通类型属性
13. 完全注解开发
创建配置类替代xml配置文件
Configuration //配置类替代xml配置文件ComponentSean(basePackage{}) //包扫描public class SpringConfig{
}编写测试类
ApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class);