【Spring篇】三.springBean自动装配及Resources

1. 自动装配(Autowiring)

类型

1.1 No : 不做任何操作
1.2 byName : 根据属性名自动装配。根据容器中bean名字查找与属性完全一致的bean,将其自动装配。bean中name重复会报错。
定义dao和service
public class TestDao {
    public void say(String word){
        System.out.println("word = [" + word + "]");
    }
}
public class TestService {
    private TestDao testDao;
    // 提供setter方法
    public void setTestDao(TestDao testDao) {
        this.testDao = testDao;
    }
    // 调用dao中的say()
    public void say(String word){
        this.testDao.say(word);
    }
}
创建spring-autowiring.xml 在beans中声明default-autowire=“byName”
<?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"
    default-autowire="byName">
    <bean id="testDao" class="com.wpj.autowiring.dao.TestDao"></bean>
    <bean id="testService" class="com.wpj.autowiring.service.TestService"></bean>
</beans>
定义测试类
@Test
public void testByName(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring-autowiring.xml");
    TestService testService = ac.getBean("testService",TestService.class);
    testService.say("this is word");
}
1.3 byType : 根据class自动装配。bean中class重复会报错,并指出不能使用该方式进行自动装配。如果没有找到相匹配则什么都不发生。
dao和service不变,xml中bean中声明default-autowire=“byType” 声明dao的bean的name或id可以去掉
<?xml version="1.0" encoding="UTF-8"?>
<beans ...
    default-autowire="byType">
    <bean class="com.wpj.autowiring.dao.TestDao"></bean>
    <bean id="testService" class="com.wpj.autowiring.service.TestService"></bean>
</beans>
1.4 Constructor : 与byType类似,不同于它应用于构造器参数。如果找不到相匹配的bean,则会报错。
在service中定义构造方法 注入dao,xml中bean中声明default-autowire=“constructor”
public class TestService {
    private TestDao testDao;
    public TestService(TestDao testDao) {
        System.out.println("TestService.TestService");
        this.testDao = testDao;
    }
    public void say(String word){
        this.testDao.say(word);
    }

}
<beans ...
    default-autowire="constructor">
    
    <bean class="com.wpj.autowiring.dao.TestDao"></bean>
    <bean id="testService" class="com.wpj.autowiring.service.TestService"></bean>
</beans>

2. Resources

针对于资源文件的统一接口

- UrlResource : URL对应的资源,根据一个URL地址即可构建
- ClassResource : 获取类路径下的资源文件
- FileSystemResource : 获取文件系统里的资源
- ServletContextResource : ServletContext封装的资源,用于访问ServletContext环境下的资源
- InputStreamResource : 针对于输入流封装的资源
- ByteArrayResource : 针对于字节数组封装的资源
ResourceLoader
 对Resource加载的接口
 注入参数的前缀
 	classpath :  从classpath中去加载
 	file : 从文件系统中去加载
 	http : 从URL去加载
 	(none) : 依赖于ApplicationContext
例子
在resources(java项目的src目录下)中创建一个test.txt 随便输点内容
定义TestResources
public class TestResources implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    public void resource() throws IOException {
        Resource resource = applicationContext.getResource("classpath:config.txt");
        // Resource resource = applicationContext.getResource("file:F:\\JavaEE1911\\ideaDevelop3\\idea\\day46\\src\\main\\resources\\config.txt");
        // Resource resource = applicationContext.getResource("url:https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference");
        // Resource resource = applicationContext.getResource("config.txt");
        System.out.println(resource.getFilename());
        System.out.println(resource.contentLength());
    }
}
在spring-resource.xml
<bean id="testResources" class="com.wpj.resources.TestResources"></bean>
Test
public class TestResource {
    @Test
    public void test(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-resource.xml");
        TestResources testResource = ac.getBean("testResources",TestResources.class);
        try {
            testResource.resource();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
发布了42 篇原创文章 · 获赞 8 · 访问量 2454

猜你喜欢

转载自blog.csdn.net/TheNew_One/article/details/103769163