bean的作用域
bean的生命周期
bean的装配
代码
实体类
package com;
import java.util.List;
public class User {
private String username;
private String password;
private List<String> list;
/**
* 设值注入要求bean类:
* 1、必须提供默认无参构造方法
* 2、为属性提供setter方法
*/
public User() {
}
public User(String username, String password, List<String> list) {
this.username = username;
this.password = password;
this.list = list;
}
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 +
'}';
}
}
dao层
package com;
import org.springframework.stereotype.Repository;
@Repository("userDao1")
public class UserDaoImpl implements UserDao{
public void say() {
System.out.println("dao");
}
}
service层
package com;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource(name="userDao1")
// @Autowired
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
this.userDao.say();
System.out.println("service");
}
}
controller层
package com;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller("userController")
public class MyController {
@Resource(name="userService")
// @Autowired
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void say(){
this.userService.save();
System.out.println("controller");
}
}
基于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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="userDao" class="com.UserDaoImpl"/>
<!-- 使用构造注入方式装配bean -->
<bean id="user1" class="com.User">
<constructor-arg index="0" value="zhangsan" />
<constructor-arg index="1" value="123" />
<constructor-arg index="2">
<list>
<value>construct1</value>
<value>construct2</value>
</list>
</constructor-arg>
</bean>
<!-- 使用设值注入方式装配bean -->
<bean id="user2" class="com.User">
<property name="username" value="lisi" />
<property name="password" value="123" />
<property name="list">
<list>
<value>set1</value>
<value>set2</value>
</list>
</property>
</bean>
</beans>
基于Annotation装配
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="userDao1" class="com.UserDaoImpl" />
<bean id="userService" class="com.UserServiceImpl" />
<bean id="userController" class="com.MyController" />
<!-- 开启注解 -->
<context:annotation-config />
</beans>
自动装配
<!-- 扫描包-->
<context:component-scan base-package="com"/>
小结
然后用@Autowired注解标注在属性上就可以实现自动装配了
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。