SpringIOC的三种配置与使用方式

SpringIOC的三种配置与使用方式

1.基于XML配置文件的bean模式

applicationContext.xml文件配置(名字可以随意,置于resources文件夹下)

         <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd></bean>

bean模式下需要在xml文件中书写的代码:

  <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd>
   <bean id="studentDao" class="com.Dao.StudentDaoImpl" ></bean>
   <bean id="studentService" class="com.services.studentSerImpl" scope="prototype">
         <property name="studentDao" ref="studentDao"></property>
         <property name="info" value="小屁孩"></property>
   </bean>
   </bean>
//dao接口
  package com.Dao;

import java.util.List;

public interface studentDao {
    
    
    List selectAllStudent();
}
//dao实现类
package com.Dao;

import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class StudentDaoImpl implements studentDao {
    
    
    @Override
    public List selectAllStudent() {
    
    
        System.out.println("查询所有学生!");
        return null;
    }
}
//services接口
package com.services;

public interface studentServices {
    void  findAllstudent();
}
//services实现类
package com.services;
import com.Dao.StudentDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class studentSerImpl implements studentServices {
    private StudentDaoImpl studentDao;
    private String info;
    //通过set,必须有set方法
    public void setInfo(String info) {
        System.out.println(info);
        this.info = info;
    }
    public void setStudentDao(StudentDaoImpl studentDao) {
        this.studentDao = studentDao;
    }
    @Override
    public void findAllstudent() {
        studentDao.selectAllStudent();
    }
}
//controller
package com.controller;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.services.*;
public class selectStudent {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext");
        studentServices studetnServices = (studentServices) applicationContext.getBean("studentService");//bean
        studetnServices.findAllstudent();
    }
}

说明:上面的bean中的id是唯一标示,class是java实现类的路径,(首先先说明一下依赖关系,controller依赖service,service依赖dao),在services中有两个成员变量,StudentDao和Info,在applicationContext.xml文件中配置bean来将所需的元素注入,services实现类中的成员变量注入的前提是在其中必须有set方法。

  <bean id="studentDao" class="com.Dao.StudentDaoImpl" ></bean>
  <bean id="studentService" class="com.services.studentSerImpl" scope="prototype">
         <property name="studentDao" ref="studentDao"></property>
         <property name="info" value="小屁孩"></property>
   </bean>

第一个bean对应的是dao实现类,第二个bean对应的是services实现类,其中的property的name是成员变量的变量名,注入对象时使用ref,注入普通变量时使用value。
bean中的scope属性,是用来控制bean的作用范围和是否是单利模式的,值有:prototype、single、request、session
bean中的autowire属性,是用来开启自动映射的,常用的值有:byType用过类型自动映射(前提是)、byName(前提是成员变量名必须和bean的id一致)
上面所述的是使用set方法注入(其实依赖的是无参构造),还有一种方法是通过构造方法注入:

   <bean id="studentService02" class="com.services.studentSerImpl">-->
       <constructor-arg index="0" ref="studentDao"/>
        <constructor-arg index="1" value="asdfsadf"/>
   </bean>
package com.services;
import com.Dao.StudentDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("studentService")
public class studentSerImpl implements studentServices {
    
    
    private StudentDaoImpl studentDao;
    private String info;
   //通过构造器,
    public studentSerImpl(StudentDaoImpl studentDao, String info) {
    
    
        System.out.println(info);
        this.studentDao = studentDao;
        this.info = info;
    }
    @Override
    public void findAllstudent() {
    
    
        studentDao.selectAllStudent();
    }
}

2.基于XML配置文件的注解方式

  首先需要扫描包在applicationcontext.xml配置文件中:
<context:component-scan base-package="com"/>
  上面的代码表示扫描com整个文件夹,
  services类:
package com.services;
import com.Dao.StudentDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("studentService")
public class studentSerImpl implements studentServices {
    
    
    @Autowired//默认
    private StudentDaoImpl studentDao;
      @Autowired//默认
    private String info;
    @Override
    public void findAllstudent() {
    
    
        studentDao.selectAllStudent();
    }
}
 // dao类:
package com.Dao;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class StudentDaoImpl implements studentDao {
    
    
    @Override
    public List selectAllStudent() {
    
    
        System.out.println("查询所有学生!");
        return null;
    }
} 
// controller类:
package com.controller;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.services.*;
public class selectStudent {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext");
        studentServices studetnServices = (studentServices) applicationContext.getBean("studentService");
        studetnServices.findAllstudent();
    }
}

controller类中的getBean中的值和Services中的注解@Component中的值保持一致,dao类中的类前面还需要加上注解@Component
基于Java的配置类的方式
基于bean的java配置类
使用java配置类的方式,就不需要xml配置文件了,需要重新建一个java类,为了好区分就叫他applicationContext,在里面书写代码如下:
applicationContext配置类:


```java
@Configuration//声明此类为配置类
public class applicationConfig {
//     @Bean(name = "getStudenDaoImpl")
//      public StudentDaoImpl getStudenDaoImpl(){
//         return   new StudentDaoImpl();
//      }
//     @Bean(name = "getStudentSerImpl")
//      public studentSerImpl getStudentSerImpl(){
//          studentSerImpl studentSer = new studentSerImpl();
//          studentSer.setInfo("ASAS");
//          studentSer.setStudentDao(getStudenDaoImpl());
//          return studentSer;
//      }

}

 dao类和上面的模式一致(需要加上@Component);
 services类也是一致(需要加上@Component);

```java
 controller类:
package com.controller;
import com.config.applicationConfig;
import com.services.studentServices;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class selectStudent02 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext(applicationConfig.class);
        studentServices studetnServices = (studentServices) acc.getBean("studentService");//bean
        studetnServices.findAllstudent();
    }
}

说明:此种方式就不能在使用加载xml配置文件的形式来获取操作类了,因为此时就没有xml配置文件了,使用需要加载java配置类,
之前的使用的加载配置文件的类是:

ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext");

使用java配置文件使用的是:

 AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext(applicationConfig.class);

3.基于注解的java配置类

applicationContext配置类:

@Configuration//生命此类为配置类
@ComponentScan("com")//扫描包
public class applicationConfig {
    
    
}

services类:
@Component("studentService")
public class studentSerImpl implements studentServices {
    
    
    @Autowired//默认
    private StudentDaoImpl studentDao;
    private String info;
    @Override
    public void findAllstudent() {
    
    
        studentDao.selectAllStudent();
    }
}
controller类:
public class selectStudent02 {
    
    
    public static void main(String[] args) {
    
    
        AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext(applicationConfig.class);
        studentServices studetnServices = (studentServices) acc.getBean("studentService");//bean
        studetnServices.findAllstudent();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42785250/article/details/103377277