Spring-IOC【04-Java配置-自动注入】

原创
2019/04/17 21:05
阅读数 7

SpringBoot流行之后,Java 配置开始被广泛使用。

Java配置本质上,就是使用一个Java类去代替xml配置,这种配置方式在SpringBoot中得到了广泛的使用。

所需jar包

在这里插入图片描述

创建实体类

package com.sxt.pojo;
/**
 * 通过设值注入的方式实现属性的设置
 * @author Administrator
 *
 */
public class User {

	public void say(){
		System.out.println("hello");
	}
}

java配置文件

java代码配置 Java配置和XML配置基本一致,唯一不同的地方就是包扫描的方式。 四个注解是一样的。 包扫描通过@ComponentScan来实现

package com.sxt.pojo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * Java配置文件
 * @Configuration 让当前类和applicationContext.xml文件
 * @author Administrator
 *
 */
@Configuration
public class JavaConfig {

	/**
	 * @Bean的作用和<bean>的作用是一样的
	 * 默认 id为方法名,可以通过name和value属性自定义
	 * @return
	 */
	@Bean("aaa")
	public User getUser() {
		User user = new User();
		return user;
	}
}

测试

package com.sxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.sxt.pojo.JavaConfig;
import com.sxt.pojo.User;

public class Demo {

	public static void main(String[] args) {
		//初始化容器
		ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);
//		User user = ac.getBean(User.class);
		User user = (User) ac.getBean("aaa");
		user.say();
	}
}

在这里插入图片描述

自动注入[XML]

前面这种配置方式,对于所有要使用的类都需要一个一个的配置,可以通过自动配置来简化Bean的配置 在这里插入图片描述

xml文件配置

xml配置通过四个注解来实现,目前来说功能是一样的 在这里插入图片描述 1. 需要在application.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:p="http://www.springframework.org/schema/p"
	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 http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 	<!-- 开启扫描 Component Controller Service Repository -->
 	<context:component-scan base-package="com.sxt.controller" use-default-filters="false">
 		<!-- 包含 -->
 		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 	</context:component-scan>
 	
 	<context:component-scan base-package="com.sxt.service.impl,com.sxt.dao.impl" use-default-filters="true">
 		<!-- 排除 -->
 		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 	</context:component-scan>
 	<!-- <context:component-scan base-package="com.sxt.*.impl"></context:component-scan>
 	<context:component-scan base-package="com.sxt.dao.impl,com.sxt.service.impl,com.sxt.controller.impl"></context:component-scan>
 	<context:component-scan base-package="com.sxt.dao.impl"></context:component-scan>
 	<context:component-scan base-package="com.sxt.service.impl"></context:component-scan>
 	<context:component-scan base-package="com.sxt.controller.impl"></context:component-scan> -->
 </beans>

use-default-filters 表示使用使用spring默认提供的过滤器, false表示不使用,true则表示使用。 一般来说, true结合exclude-filter标签使用,表示除去某个注解 false结合include标签使用,表示包含某个注解

package com.sxt.dao;

public interface IUserDao {

	public String say(String msg);
}
package com.sxt.dao.impl;

import org.springframework.stereotype.Repository;

import com.sxt.dao.IUserDao;
//@Component //等价于<bean class="com.sxt.service.impl.UserDaoImpl" name="userDaoImpl"/>
@Repository
public class UserDaoImpl implements IUserDao{

	@Override
	public String say(String msg) {
		System.out.println("--->"+msg);
		return "hello";
	}
}
package com.sxt.service;

public interface IUserService {

	public String say(String msg);
}
package com.sxt.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.sxt.dao.IUserDao;
import com.sxt.service.IUserService;

//@Component //当前类交给Spring容器管理
@Service("aaa")
public class UserServiceImpl implements IUserService{

	//@Resource //从容器中获取实例赋值
	@Autowired //只能根据类型查找
	@Qualifier("userDaoImpl") //@Autowired要通过name查找需要和@Qualifier一起使用
	private IUserDao dao;

	@Override
	public String say(String msg) {
		// TODO Auto-generated method stub
		return dao.say(msg);
	}
}
package com.sxt.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.sxt.service.IUserService;
//@Component
@Controller
public class UserContreller {

	@Resource(name="aaa")
	private IUserService userService;

	public void say(String msg){
		System.out.println("controller:"+userService.say(msg));
	}
	
	public void sleep(){
		System.out.println("休息一下");
	}
}
package com.sxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxt.controller.UserContreller;

public class TestDemo {

	public static void main(String[] args) {
		ApplicationContext ac =
				new ClassPathXmlApplicationContext("applicationContext.xml");
		UserContreller user = ac.getBean(UserContreller.class);
		user.say("aaa");
	}
}

在这里插入图片描述

自动注入[Java]

在这里插入图片描述

package com.sxt;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;

@Configuration
//添加扫描
//@ComponentScan("com.sxt.*")
@ComponentScans(value={@ComponentScan(""),@ComponentScan(""),@ComponentScan("")})
/*特殊扫描
@ComponentScan(value="com.sxt.*",useDefaultFilters=true,
	excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes=Service.class)})*/
public class JavaConfig {

}
package com.sxt.dao;

public interface IUserDao {

	public String say(String msg);
}
package com.sxt.dao.impl;

import org.springframework.stereotype.Repository;

import com.sxt.dao.IUserDao;
//@Component //等价于<bean class="com.sxt.service.impl.UserDaoImpl" name="userDaoImpl"/>
@Repository
public class UserDaoImpl implements IUserDao{

	@Override
	public String say(String msg) {
		System.out.println("--->"+msg);
		return "hello";
	}
}
package com.sxt.service;

public interface IUserService {

	public String say(String msg);
}
package com.sxt.service.impl;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.sxt.dao.IUserDao;
import com.sxt.service.IUserService;

//@Component //当前类交给Spring容器管理
@Service("aaa")
public class UserServiceImpl implements IUserService{
	//@Resource //从容器中获取实例赋值
	@Autowired //只能根据类型查找
	@Qualifier("userDaoImpl") //@Autowired要通过name查找需要和@Qualifier一起使用
	private IUserDao dao;
	@Override
	public String say(String msg) {
		// TODO Auto-generated method stub
		return dao.say(msg);
	}
}
package com.sxt.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.sxt.service.IUserService;
//@Component
@Controller
public class UserContreller {

	@Resource(name="aaa")
	private IUserService userService;

	public void say(String msg){
		System.out.println("controller:"+userService.say(msg));
	}
	public void sleep(){
		System.out.println("休息一下");
	}
}
package com.sxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sxt.JavaConfig;
import com.sxt.controller.UserContreller;

public class TestDemo {
	public static void main(String[] args) {
		ApplicationContext ac =
				new AnnotationConfigApplicationContext(JavaConfig.class);
		UserContreller user = ac.getBean(UserContreller.class);
		user.say("aaa");
	}
}

在这里插入图片描述

@Resource和@Autowired的区别

在这里插入图片描述

展开阅读全文
打赏
4
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
4
分享
返回顶部
顶部