1:
配置文件:
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描包,扫描含有注解的类,spring容器会实例化该注解的类-->
<context:component-scan base-package="com.example.springtest.configration"></context:component-scan>
</beans>
package com.example.springtest.configration.repository;
import org.springframework.stereotype.Component;
/**
* ***GOOD LUCK****
*
* @Author : Wukn
* @Date : 2018/6/
* @Component注解会被ioc识别,并且在@Component中可以指定bean的名称,要是不指定会默认是类名,但是第一个字母是小写
*/
@Component
public class UserRepositoryImpl implements UserRepository{
@Override
public void addU() {
System.out.println("eee");
}
}
:2:
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<!-- type="annotation" 表示是通过类路径的方式
base-package扫描包,扫描含有注解的类,spring容器会实例化该注解的类
resource-pattern 会指定只扫描某个包下面的某个类
context:exclude-filter 子节点指定排除哪些指定表达式组件
context:include-filter 只包含 在使用这个时,,需要use-default-filters="false"指定false
-->
<context:component-scan
base-package="com.example.springtest.configration" use-default-filters="false">
<!--<context:exclude-filter type="annotation" expression="com.example.springtest.configration.TestObject"></context:exclude-filter>-->
<context:include-filter type="annotation" expression="com.example.springtest.configration.epository"></context:include-filter>
</context:component-scan>
<!-- type="assignable" 表示是通过类名的方式
base-package扫描包,扫描含有注解的类,spring容器会实例化该注解的类
resource-pattern 会指定只扫描某个包下面的某个类
context:exclude-filter 子节点指定排除哪些指定表达式组件
context:include-filter 只包含 在使用这个时,,需要use-default-filters="false"指定false
-->
<context:component-scan
base-package="com.example.springtest.configration" use-default-filters="false">
<!--type="assignable" 表示是通过类名的方式
<context:exclude-filter type="annotation" expression="com.example.springtest.configration.TestObject"></context:exclude-filter>-->
<context:include-filter type="assignable" expression="com.example.springtest.configration.repository.UserRepository"></context:include-filter>
</context:component-scan>
</beans>
package com.example.springtest.web.controller;
import com.example.springtest.configration.repository.UserRepository;
import com.example.springtest.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
/**
* ***GOOD LUCK****
*
* @Author : Wukn
* @Date : 2018/6/
*/
@RestController
@RequestMapping(value = "test")
public class TestController {
/**
* 在ioc容器中,要是没有装配成功这个bean会有异常抛出,是设置了required = false就返回null
* @Qualifier("userRepositoryImpl") 会指定装配的bean
* 也可以用set方法
*/
@Autowired(required = false)
@Qualifier("userRepositoryImpl")
private UserRepository userRepository;
//
// public void setUserRepository( @Qualifier("userRepositoryImpl")UserRepository userRepository) {
// this.userRepository = userRepository;
// }
@PostMapping(value = "gets")
@ResponseBody
public String getStrinf(@RequestBody User user) {
userRepository.addU();
return user.getName()+ user.getId( );
}
}