1.在classpath当中扫组件
Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件
特定组件包括:
@Component:基本组件
@Respository:标识持久层组件
@Service:标识服务层组件
@Controller:标识表现层组件
开启注解:<context:component-scan/>
<context:component-scan/>该元素还会自动注册AutowireAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowire,@Resource,@Inject注解的属性。
<?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-4.3.xsd">
<!--
指定Spring IOC容器要扫描的包
可以通过resourc指定扫描的资源(此处是扫描repository下的所有类),注意是指定包,不会扫描其子包
resource-pattern="repository/*.class"
context:exclude-filter指定排除那些表达式组件
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<context:component-scan base-package="com.sprign.annotiction" use-default-filters="false">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> -->
<!-- 指定包含哪些组件的时候,一定要设置use-default-filters="false",否则全包括 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
*********************************************************************
package com.sprign.annotiction;
import org.springframework.stereotype.Component;
@Component
public class TestObject {
}
*********************************************************************
package com.sprign.annotiction.repository;
public interface UserRepository {
public void save();
}
*********************************************************************
package com.sprign.annotiction.repository.impl;
import org.springframework.stereotype.Repository;
import com.sprign.annotiction.repository.UserRepository;
@Repository
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository Save...");
}
}
*********************************************************************
package com.sprign.annotiction.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add() {
System.out.println("UserServiece add...");
}
}
*********************************************************************
package com.sprign.annotiction.controller;
import org.springframework.stereotype.Controller;
@Controller
public class UserCotroller {
public void execute() {
System.out.println("UserController execute...");
}
}
*********************************************************************
package com.sprign.annotiction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sprign.annotiction.controller.UserCotroller;
import com.sprign.annotiction.repository.UserRepository;
import com.sprign.annotiction.service.UserService;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject to=(TestObject) ctx.getBean("testObject");
UserCotroller uc=(UserCotroller) ctx.getBean("userCotroller");
UserService us=(UserService) ctx.getBean("userService");
// UserRepository ur=(UserRepository) ctx.getBean("userRepositoryImpl");
System.out.println(to);
System.out.println(uc);
System.out.println(us);
// System.out.println(ur);
}
}
1.Resources
主要用加载在资源文件,通过Spring加载一些资源文件的时候可以使用Resources来完成
image.png
通过ApplicationContext对象的getResource()方法来加载资源文件到Spring当中
image.png
加载资源文件的方式
image.png
2.Classpath扫描注解
image.png
如果要扫描到注册在类上的注解,必须要使用如下载的扫描器在配置文件当中进行设置。
image.png
image.png
<context:annotation-config/>只会扫描加载在方法或者成员变量上面的注解,不会扫描类上面的注解。
image.png
image.png
image.png
image.png
3.Autowired
这个注解即可以加在属性上,也可以加载在方法上,在进行匹配的时候会先按照"Name"进行匹配,如果不匹配就会按照"type"进行匹配,要注意的是,如果指定了name那就会不会在去按照"type"匹配,如果不匹配就会报错。注意Autowired注解没有name属性,可以结合Qualifier(value="")来指定name。
image.png
image.png
image.png
4.Required注解
Required注解不是spring提供的,他是javax提供的注解
image.png
5.基于java的容器注解
即使用bean注解,主要在@Component与@Configuration注解的类当中使用@Bean注解,但是@Component注解很少使用。
image.png
image.png
image.png
image.png
6.Resource注解
@Resounce注解是javax的注解,加在属性或者方法上面
在匹配的时候先按照'name'匹配,在按照'Type'匹配,在加在set方法上面的时候,三者只有有一个匹配就会匹配。如果设置了name,如果不匹配就会报错,不会在去检查type,这个注解自己自带了name属性。
与Resource大致用法一样,都javax提供的
image.png
image.png