spring IOC 控制反转详解

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名称进行装配
  <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>






  
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值