spring框架xml的几种配置方式

ioc配置一般由一下两大类

  • 1 手动装配
    利用xml手动配置,分为:setter方法装配 ,构造器装配
  • 2 自动装配
    利用注解自动装配

准备工作:userdao userservice

package com.woniu.spring.ioc.dao;
/*
创建一个接口,用于实现
 */
public interface UserDao {
    //声明方法
    public void find();
}
package com.woniu.spring.ioc.dao.impl;

import com.woniu.spring.ioc.dao.UserDao;
//实现类
public class UserDaoImpl implements UserDao {
    @Override
    public void find() {
        System.out.println("调用find方法");
    }
}
package com.woniu.spring.ioc.service;
//接口
public interface UserService {

    public void find();
}

1 手动装配

1 通过setter方法注入,需要配置set方法

package com.woniu.spring.ioc.service.impl;

import com.woniu.spring.ioc.dao.UserDao;
import com.woniu.spring.ioc.dao.impl.UserDaoImpl;
import com.woniu.spring.ioc.service.UserService;

public class UserServiceImpl implements UserService {
    //以前的写法,直接new对象,然后调用方法
    //UserDao userDao=new UserDaoImpl();

    //ioc写法,不赋值,只声明,值由spring容器注入
    UserDao userDao;
    //需要添加setter方法,spring容器通过调用set方法注入依赖
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void find() {
        userDao.find();
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    创建bean,spring中一个bean就是一个实例.牢记创建实例和注入依赖两个步骤-->
    <!--    在userServiceiml类中需要调用userdaoiml,所以我们先创建userdaoiml的实例-->
    <!--    bean格式如下 id是标记,方便后续引用该实例,class是类路径-->
    <bean id="userDao" class="com.woniu.spring.dao.impl.UserDaoImpl"/>

    <!--    接下来是实例化userserviceiml 并为其usrdao成员属性注入依赖-->
    <bean id="userService" class="com.woniu.spring.service.impl.UserServiceImpl">
        <!--        注入依赖,通过property标签  name属性会匹配类中的set方法 ref通过id名引用上面实例的bean,通过set方法注入依赖-->
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

2 通过构造器注入,

package com.woniu.spring.service.impl;

import com.woniu.spring.dao.UserDao;
import com.woniu.spring.service.UserService;

public class UserServiceImpl implements UserService {
    /*public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }*/
    //有参构造
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    UserDao userDao;

    @Override
    public void find() {
        userDao.find();
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    创建bean,spring中一个bean就是一个实例.牢记创建实例和注入依赖两个步骤-->
    <!--    在userServiceiml类中需要调用userdaoiml,所以我们先创建userdaoiml的实例-->
    <!--    bean格式如下 id是标记,方便后续引用该实例,class是类路径-->
    <bean id="userDao" class="com.woniu.spring.dao.impl.UserDaoImpl"/>

    <!--    接下来是实例化userserviceiml 并为其usrdao成员属性注入依赖-->
    <bean id="userService" class="com.woniu.spring.service.impl.UserServiceImpl">
<!--        通过constructor-arg 注入依赖 name对应类的成员属性 ref对应引用-->
       <constructor-arg name="userDao" ref="userDao"/>
    </bean>
</beans>

3 通过Java类配置文件,单独建一个配置类,代替xml

配置class文件

package com.woniu.spring;

import com.woniu.spring.dao.UserDao;
import com.woniu.spring.dao.impl.UserDaoImpl;
import com.woniu.spring.service.UserService;
import com.woniu.spring.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//此类需要加注解,用于声明是配置文件
@Configuration
public class ConfigContext {
    @Bean
    //加注解声明是bean,通过方法创建对象
    public UserDao userDao(){
        return new UserDaoImpl();
    }
    @Bean
    //注入依赖可以传参调用set方法,也可以用构造器注入,此处用构造器调UserDao()方法注入
    //也可以调用bean对应方法
    public UserService userService() {
        UserService userService = new UserServiceImpl(userDao());
        return userService;
    }

}

主方法

package com.woniu.spring;

import com.woniu.spring.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        /*ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = ac.getBean("userService", UserService.class);
        userService.find();*/
//通过AnnotationConfigApplicationContext解析ConfigContext配置类文件
        ApplicationContext ac=new AnnotationConfigApplicationContext(ConfigContext.class);
        UserService userService=ac.getBean(UserService.class);
        userService.find();

    }
}

自动装配

1 利用xml扫描所有类文件,对应类文件加上注解
@controller @service @Repository 分别对应controller service dao类
@autowired自动注入依赖

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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    配置路径,扫描所有类文件-->
    <context:component-scan base-package="com.woniu.spring"/>
</beans>

Userdao

package com.woniu.spring.dao;

import org.springframework.stereotype.Repository;
@Repository
 public interface UserDao {
    public void find();
}
package com.woniu.spring.dao.impl;

import com.woniu.spring.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public void find() {
        System.out.println("调用find方法");
    }
}

Userservice

package com.woniu.spring.service;

import org.springframework.stereotype.Service;

@Service
public interface UserService {
    public void  find();
}
package com.woniu.spring.service.impl;

import com.woniu.spring.dao.UserDao;
import com.woniu.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    //set
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    //有参构造
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }
    @Autowired
    UserDao userDao;

    @Override
    public void find() {
        userDao.find();
    }
}

主方法

package com.woniu.spring;

import com.woniu.spring.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = ac.getBean("userService", UserService.class);
        userService.find();

//通过AnnotationConfigApplicationContext解析ConfigContext配置类文件
        /*ApplicationContext ac=new AnnotationConfigApplicationContext(ConfigContext.class);
        UserService userService=ac.getBean(UserService.class);
        userService.find();*/

    }
}

2 利用配置类文件扫描所有类,加注解
配置类,提带xml,利用注解扫描所有类
其他依然是@controller @service …@auotwired注解

package com.woniu.spring;

import com.woniu.spring.dao.UserDao;
import com.woniu.spring.dao.impl.UserDaoImpl;
import com.woniu.spring.service.UserService;
import com.woniu.spring.service.impl.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//此类需要加注解,用于声明是配置文件
@Configuration
//配置扫描所有类,加入包路径,其他类不变
@ComponentScan("com.woniu.spring")
public class ConfigContext {
    /*@Bean
    //加注解声明是bean,通过方法创建对象
    public UserDao userDao(){
        return new UserDaoImpl();
    }
    @Bean
    //注入依赖可以传参调用set方法,也可以用构造器注入,此处用构造器调UserDao()方法注入
    //也可以调用bean对应方法
    public UserService userService() {
        UserService userService = new UserServiceImpl(userDao());
        return userService;
    }*/


}

主方法

package com.woniu.spring;

import com.woniu.spring.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
      /*  ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = ac.getBean("userService", UserService.class);
        userService.find();*/

//通过AnnotationConfigApplicationContext解析ConfigContext配置类文件
        ApplicationContext ac=new AnnotationConfigApplicationContext(ConfigContext.class);
        UserService userService=ac.getBean(UserService.class);
        userService.find();

    }
}