您的位置:首页 > 编程语言 > Java开发

spring IOC 控制反转详解

2017-02-28 19:29 113 查看
spring  IOC  AOP

IOC 控制反转:在使用一个类前,需要创建一个对象的实例,IOC将创建实例交给IOC容器;

1.spring应用开发企业级项目 采用的方式 ApplicationContext

2.ApplicationContext有三个实现类

第一个实现类:ClassPathXmlApplicationContext 它是从当前路径中检索配置文件并装载它来创建容器的实例

   ApplicationContext context = new ClassPathXmlApplicationContext(String configLocation);

第二个实现类FileSystemXmlApplicationContext 通过参数指定配置文件的位置,可以访问类路径之外的其他资源

    ApplicationContext context = new FileSystemXmlApplicationContext(String configLocation)

第三个实现类:WebApplicationContext ,它是spring的web应用容器,有两种方法可以在servlet中使用WebApplicationContext,第一种方法是在servlet的web.xml文件配置Spring的ContextLoaderListener监听器,第二种修改web.xml配置文件,在配置文件里添加一个servlet,定义使用Spring的org.springframework.web.context.ContextLoaderServlet类

3.依赖注入

  第一、接口注入(不常用)

  第二种,Setter注入,变量私有化,提供一个set和get方法(常用)

  第三种,构造器注入:使用的是构造方法

      public  class User{

         private String name;

         private String password;

         public User(String name,String password){

                   this.name=name;

                   this.password = password;

     }

}

4.bean的配置

  无论使用哪种容器,都要从配置文件中读取javaBean定义的信息

  <bean id="test" class="com.james.Test.User">

   id属性为bean的名称,class属性为对应类的类名,同过BeanFactory容器的getBean("test")方法就可以得到该类的实例

5.Setter注入

  第一步:每个私有属性对应一个Setter()和Getter()方法,实现对属性的封装

  第二步:配置文件中的<property>元素为JavaBean的Setter方法传参

  public class User {

  private String name;

  private String password;

  private int age;

  。。。。。。。

 applicationContext.xml配置

  <!-- 配置bean -->

<bean id="user" class="com.james.entity.User">

 <property name="name" value="lucy"></property>

 <property name="password" value="123456"></property>

 <property name="age" value="27"></property>

</bean>

 测试类:

 public class TestUser {

 public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("user");

System.out.println(user);

}

}

6.构造器注入

  在类被实例化时,构造方法被调用并且只调用一次,构造器用户对类的初始化操作

  <constructor-arg>是Bean的子元素,通过<constructor-arg>元素<value>对构造方法传参

  User2类

public class User2 {

  private String name;

  private String password;

  private int age;

  

public User2(String name, String password, int age) {
super();
this.name = name;
this.password = password;
this.age = age;

}

配置 bean

<bean id="user2" class="com.james.entity.User2">

  <constructor-arg>

     <value>jame</value>

  </constructor-arg>

  <constructor-arg>

     <value>1234567</value>

  </constructor-arg>

  <constructor-arg>

     <value>20</value>

  </constructor-arg>

</bean>

测试类

public class TestUser {

 public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User2 user = (User2)context.getBean("user2");

System.out.println(user);

}

}

7.引用其他 bean

创建manager类

  public class Manager {

   private User2 user;

public User2 getUser() {
return user;

}

public void setUser(User2 user) {
this.user = user;

}

配置 bean

<bean id="manager" class="com.james.controller.Manager">

  <property name="user" ref="user2"></property>

</bean>

测试类

public class TestManager {

   public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Manager manager = (Manager)context.getBean("manager");
manager.getUser().printInfo();

}

}

}

自动加载配置文件

 <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

<!-- 配置servlet自启动spring框架里的包 -->

 <servlet>

  <servlet-name>springmvc</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <load-on-startup>1</load-on-startup>

 </servlet>

 <servlet-mapping>

   <servlet-name>springmvc</servlet-name>

   <!-- 进入springmvc框架的匹配规则 可以用/  /xxx/*    /*.xxx  不可以用/*-->

   <url-pattern>/</url-pattern>

 </servlet-mapping>

8.自动装配:将一个bean注入到其他Bean的property中

 --->按照Bean名称进行装配

 
9fd6
<bean autowire="byName" id="printInfo" class="com.james.controller.PrintInfo"></bean>

 --->按照bean类型进行装配

<bean autowire="byType" id="printInfo" class="com.james.controller.PrintInfo"></bean>

9.bean的作用域scope="singleton" scope="prototype"

 singleton 只要id与bean定义相匹配,返回bean的单一实例,饮水机喝水

 prototype作用域 每次返回新的实例化对象

AOP

1.持久化操作

   

<!-- 配置数据源 -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/usertest</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<!-- 配置数据源结束 -->

<!-- 为UserDao注入数据源 -->
<bean id="userDao" class="com.leige.dao.UserDao">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--为UserService配置bean  -->
<bean id="userService" class="com.leige.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>

<bean id="userController" class="com.leige.controller.UserController">
<property name="userService" ref="userService"></property>

</bean>

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: