1.bean文件配置:
[html] www.2cto.com
2.
Java代码
[java]
@Autowired
private UserDao userdao;
这样就可以调用UserDao里的方法了
因为dao和service在一个项目中会有多个,这就要写很多个类似的配置,下面以service为例说下如何实现批量自动装载,我们假设所有service文件放在com.test.biz下
1.目录结构
2.bean文件配置
[
html]
< xml version="1.0" encoding="UTF-8" >
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://localhost:8080/schema/www.springframework.org/schema/context/spring-context.xsd
"
default-autowire="byName"> ①
①设置自动装载为按名称
②扫描的包
③配置的文件
3. User.java
[java]
package com.test.biz;
public interface User {
String getName();
}
4. UserImpl.java
[java]
package com.test.biz.impl;
import com.test.biz.User;
public class UserImpl implements User{
private String name = "aaa";
public String getName(){
return this.name;
}
}
5. Java中使用
[java]
@Autowired
private User user;
6.这样就完成了,以后如果有新的service只要在biz目录里写server文件即可,不需要再配置bean文件了,方便吧~