IOC的概念:
说是概念不如说是作用,IOC主要的两个作用就是控制反转,和依赖注入; 控制反转:以往我们创建对象都是由直接new 对象,而Spring的控制反转就避免了我们 new对象,而是从调用的类反转到Spring容器,由Spring来创建对象 依赖注入:由Spring来给属性赋值,也是通过Spring容器来实现IOC配置方式: 通过maven工程的pom.xml导入Spring-Context依赖,并且 通过File 里的XML Spring Config 创建Spring ioc的配置文件,或者通过注解JAVA类来完成IOC的配置文件配置 IOC实例化的几种方式: 1.通过XML里的bean来实例化
<bean id="serviceController" class="com.cdsn.spring.ServiceController"/>//这是默认构造器实例化 //无参构造实例化,只需要class关联类即可2.用静态工厂方法进行实例化
<bean id="serviceController" class="com.woniu.spring.StaticServer" factory-method="ss"/> //通过关联的Class类里的静态方法进行实例化对象,这个对象不一定就是关联类的本类对象 //也可以是其他对象 public class StaticServer { public static String ss(){ return new String(); } }3.工厂实例化
<bean id="serviceController" class="com.csdn.spring.SerDao"/> <bean factory-bean="serviceController" factory-method="Server"/> //工厂模式实例化是一个bean的实例化并不由他本身实例化,而是通过关联另一个类, //并调用关联类的方法来完成实例化 public class SerDao { public Sers Server(){ return new Sers(); } } public class Sers { }4.通过java类实现接口来实例化
<bean id="serviceController" class="com.csdn.spring.ServiceController"/> public class Demo implements FactoryBean{ @Override public Object getObject() throws Exception { return new App(); } @Override public Class<?> getObjectType() { return App.class; } } //重写两种方法,通过getObject方法获得对象,getObjectTyoe方法得到对象类型IOC:依赖注入的方式 1.通过setter方法注入
public class Ds { private Sers sers; public void setSers(Sers sers) { this.sers = sers; } } <bean id="Sers" class="com.csdn.spring.sers"> <bean id="server" class="com.csdn.spring.ds"> <property name="sers" ref="Sers"> </property> </bean>这是一般对象注入方式,基本数据类型可把ref 改为 value即可设置 而一些比较特殊的接下来会讲
<bean id="server" class="com.csdn.spring.ds"> <property name="list" > <list> <value>值一</value> <value>值二</value> </list> </property> </bean> <bean id="server" class="com.csdn.spring.ds"> <property name="map" > <map> <entry key="1" value="ss"> <entry key="2" value="ss"> </map> </property> </bean>其中property的name默认调用的是属性的setter方法,name填入需要注入的属性名即可 2.构造器方式注入 选用构造器方式注入需把无参构造器给去掉,添加有参构造器即可
<bean id="Sers" class="com.csdn.spring.sers"> <bean id="server" class="com.csdn.spring.ds"> <construct-arg name="sers" index="0" ref="Sers"> </property> </bean>这是对象注入,可用index下标确定是第几个构造器的参数,进行注入,也可以直接用 name 确定参数。
<bean id="server" class="com.csdn.spring.ds"> <construct-arg name="list" > <list> <value>值一</value> <value>值二</value> </list> </construct-arg> </bean> <bean id="server" class="com.csdn.spring.ds"> <construct-arg name="map" > <map> <entry key="1" value="ss"> <entry key="2" value="ss"> </map> </construct-arg> </bean>特殊的基本都和setter方法一样,就是把property替换成了construct-arg
作用域有五个这里详细讲2个 五大作用域Singleton单例,prototype原型,request一次请求,Session一次会话,webSocket一次链接 本次主要讲下Singleton单例
public static void main( String[] args ) { ApplicationContext cs = new ClassPathXmlApplicationContext("logs.xml"); Sers sers = cs.getBean("serviceController", Sers.class); Sers sers1 = cs.getBean("serviceController", Sers.class); System.out.println(sers); System.out.println(sers1); } <bean id="serviceController" scope="singleton" class="com.csdn.spring.Writs.Sers"/>执行结果是这样的 com.csdn.spring.Writs.Sers@204f30ec com.csdn.spring.Writs.Sers@204f30ec
两个结果地址相同说明是同一个实例,不管有多少个创建实例只要设置scope为单例就只会创建一个实例,多次创建就会调用同一个实例,不设置scope默认是单例。 prototype原型
<bean id="serviceController" scope="prototype" class="com.csdn.spring.Writs.Sers"/>还是同一份java代码 结果却是:com.csdn.spring.Writs.Sers@42e26948 com.csdn.spring.Writs.Sers@57baeedf 地址不同说明不是同一个实例,说明是两个实例
生命周期分为两种方式,一种是在bean标签里面套入init-method destroy-method 他会在容器创建时调用init-method里的方法,容器销毁时调用destroy-method里的方法。
<bean id="serviceController" init-method="start" destroy-method="end" scope="prototype" class="com.csdn.spring.Writs.Sers"/>这是一种方式,接下来讲另一种方式 通过java类实现接口
public class Sers implements InitializingBean, DisposableBean { @Override public void destroy() throws Exception { } @Override public void afterPropertiesSet() throws Exception { } }实现InitializingBean和DisposableBean接口重写afterpropertiesSet和Destroy方法 即可实现在容器创建时调用afterPropertiesSet方法,容器销毁时调用Destroy方法。
IOC前面提过xml配置方式,他的配置方式也太过于繁琐,接下来讲下注解方式和java配置方式 注解方式: 还是要xml文件不过只需要一句话
<context:component-scan base-package="包名">他会递归进行对指定包下面扫描 接下来讲下几个常用的注解 @Controller 控制层 加在类上 @Service 服务层 加在类上 @Repository dao层 加在类上 @Component 除以上三层外的 加在类上 @Autowired 自动注入依赖加在属性上 @value()设定值给属性 @PostConstruct == init-method @PreDestroy == destroy-method
接下来讲一下java类实现,抛弃掉xml 需要在java类上加上注解Configuration 识别他是一个配置类 @Bean注解在方法上恒等于bean标签,Bean标记的必须要有返回值,方法参数为依赖请求 @ComponentScan()扫描包也是在类上面配置(“路径”) @PropertySource可读取properties文件,之后用spring的EL表达式进行取值 Java主类里不再用ClasspathxmlApplication读取 用new AnnotationConfigApplicationContext读取类文件 @import导入另一个java类配置 @importResource导入一个xml配置文件
