アセンブリ1.自動(オートワイヤリング)
タイプ
1.1 No : 不做任何操作
1.2 byName : 根据属性名自动装配。根据容器中bean名字查找与属性完全一致的bean,将其自动装配。bean中name重复会报错。
ダオやサービスの定義
public class TestDao {
public void say(String word){
System.out.println("word = [" + word + "]");
}
}
public class TestService {
private TestDao testDao;
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public void say(String word){
this.testDao.say(word);
}
}
豆には、ばねautowiring.xml宣言デフォルト-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重复会报错,并指出不能使用该方式进行自动装配。如果没有找到相匹配则什么都不发生。
名前またはIDを削除することができますDAOやサービス変更、豆のdefault-autowire =「byType」文DAO Bean内のXML宣言
<?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,则会报错。
豆デフォルト-autowire =「コンストラクタ」でサービスコンストラクタ注入DAO、XML宣言の定義で
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.リソース
リソースファイルへの統一されたインタフェースのための
- UrlResource : URL对应的资源,根据一个URL地址即可构建
- ClassResource : 获取类路径下的资源文件
- FileSystemResource : 获取文件系统里的资源
- ServletContextResource : ServletContext封装的资源,用于访问ServletContext环境下的资源
- InputStreamResource : 针对于输入流封装的资源
- ByteArrayResource : 针对于字节数组封装的资源
ResourceLoader
对Resource加载的接口
注入参数的前缀
classpath : 从classpath中去加载
file : 从文件系统中去加载
http : 从URL去加载
(none) : 依赖于ApplicationContext
例
(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");
System.out.println(resource.getFilename());
System.out.println(resource.contentLength());
}
}
春-resource.xmlで
<bean id="testResources" class="com.wpj.resources.TestResources"></bean>
テスト
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();
}
}
}