bean的装配方式(注入方式,构造注入,setter属性注入)
bean的装配方式有两种,构造注入和setter属性注入。
public class User {
private String username;
private String password;
private List<String> list;
//构造注入需要提供带所有参数的有参构造方法
public User(String username, String password, List<String> list) {
this.username = username;
this.password = password;
this.list = list;
}
//设置注入,需要提供无参构造方法和setter和getter方法
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", list=" + list +
'}';
}
}
在applicationContext.xml中的配置
<!--构造方法方式装配bean-->
<!--在这儿可以采用p名称空间的形式注入值-->
<bean id="user1" class="com.itheima.ioc.xmlzhuangpeiBean.User">
<constructor-arg index="0" value="chen"/>
<constructor-arg index="1" value="123456"/>
<constructor-arg index="2">
<list>
<value>"user1"</value>
<value>"user2"</value>
</list>
</constructor-arg>
</bean>
<!--使用设置注入方式装配bean-->
<!--在这儿可以采用p名称空间的形式注入值-->
<bean id="user2" class="com.itheima.ioc.xmlzhuangpeiBean.User">
<property name="username" value="陈红君"/>
<property name="password" value="145678"/>
<property name="list">
<list>
<value>"4679"</value>
<value>"213"</value>
</list>
</property>
</bean>
<!--例如这儿的bean可以写成这样
<bean id="user2" class="类的全限定名" p:username="小城" p:password="123"/>
-->
</beans>
基于注解的装配
接口UserDao
public interface UserDao {
public void userDaoSay();
}
实现接口的USerDaoImp
@Repository("userDao")//用于数据访问层
public class UserDaoImp implements UserDao {
@Override
public void userDaoSay() {
System.out.println("这是实现userdao的方法");
}
}
接口UserService
public interface UserService {
public void userServiceSay();
}
实现接口的UserServiceImp
@Service("userService")
public class UserServiceImp implements UserService {
@Resource(name = "userDao")//按照实例名称进行装配的
private UserDao userDao;
public void userServiceSay() {
this.userDao.userDaoSay();
System.out.println("userservice to say hello world!");
}
}
控制器UserController
@Controller("userController")//控制器
public class UserController {
@Resource(name = "userService")
private UserService userService;
public void userControllerSay() {
this.userService.userServiceSay();
System.out.println("userController to Say hello world!");
}
/*实例化一个userController对象,调用userControllerSay()方法,userController 先调用userService里的方法
当执行到了userService,userService就去调用执行userDao中的userDaoSay()方法
* */
}
applicationContext.xml中的配置
<?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-3.0.xsd">
<context:component-scan base-package="com.itheima.ioc.zhujiefangshizhuangpeiBean"/>
</beans>
测试
public class annotationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("anntationContext.xml");
UserController userController = (UserController) applicationContext.getBean("userController");
userController.userControllerSay();
}
}