Spring的作用:解决类与类之间的强耦合问题。
有三种注入方式,构造方法注入,接口注入,还有set注入,其中set注入最常用,set注入代码如下
User类与email类代码如下
public class User {
private String name;
private Email email;
private List<String> hobby;
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Email getEmail() {
return email;
}
public void setEmail(Email email) {
this.email = email;
}
}
public class Email {
private String emailName;
public String getEmailName() {
return emailName;
}
public void setEmailName(String emailName) {
this.emailName = emailName;
}
}
配置文件代码如下
<bean id="user" class="spring.User" scope="prototype">
<property name="name">
<value>freshbin</value>
</property>
<property name="email">
<ref bean="emailid"/>
</property>
<property name="hobby">
<list>
<value>打代码</value>
<value>下棋</value>
</list>
</property>
</bean>
<bean id="emailid" class="spring.Email">
<property name="emailName">
<value>666666@qq.com</value>
</property>
</bean>
在使用spring时,必须建立一个application.xml配置文件,其完整内容如下
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="user"></context:component-scan>
<bean id="user" class="spring.User" scope="prototype">
<property name="name">
<value>freshbin</value>
</property>
<property name="email">
<ref bean="emailid"/>
</property>
<property name="hobby">
<list>
<value>打代码</value>
<value>下棋</value>
</list>
</property>
</bean>
<bean id="emailid" class="spring.Email">
<property name="emailName">
<value>666666@qq.com</value>
</property>
</bean>
</beans>
其中ref是指定注入另一个对象,属性则是用property标签,有name与value注入,list,set,map集合也是同理。
<context:component-scan base-package="user"></context:component-scan>这行代码是让spring对user下面的所有包进行扫描,不然程序运行时,spring注入没法实现
spring的注解如下
@Autowired按类型注解
@Qualifier按ID注解
@Controller控制层的注解
@Service业务逻辑层的注解
@Repository DAO层的注解
@Scope控制多实例还是单实例的注解,在Web应用还有request和session
@Component注解可以放在类的头上,@Component不推荐使用。
其中@Autowired是当一个类需要引入其他类时,加入的注解,如下
@Service("userServiceImpl")
@Scope("prototype")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public boolean checkLogin(String account, String pwd) {
userDao.CheckAccount();
System.out.println("UserServiceImpl.checkLogin");
return false;
}
}
当一个接口有多个实现类时,则加入@Qualifier("userServiceImpl")代表指定的实现类