演示的spring版本为4.2.4
第一天学习了spring的ioc原理与基本应用以及基于xml的注入方法。
通过构造方法注入,依赖对应的构造方法,一般用于生成导入的jar包中的类的对象。
通过set方法注入,依赖对应属性的set方法,有一般的方法注入,还有引入p名称空间,以及SpEl方法。
通过注解的方法注入, 注解注入只需要定义属性,不依赖构造方法与set方法。
注解有(还没有与SpringMVC,mybatis整合):
@Component 表示该类是受Spring管理的类,当不指定id时,默认值是类名,首字母变小写
@Controller@Service@Repository
分别代表web层,业务层,持久层,跟Component一样的功能,不过可以分层来看,一目了然
@Value 给普通字段赋值
@AutoWired 给对象赋值(根据对象类型赋值)
@Qualifer 一般和AutoWired一起使用,括号里面指定id
@Resource 根据Bean的id来注入bean的类型
@Scope 指定域的范围
singleton prototype request session globalsession
@PostConstruct 表示初始化方法
@PreDestroy 表示销毁时方法
下面给出栗子,最后是小结。
Address类(@Scope("prototype")被注释了);
package com.itheima.domain; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.io.Serializable; @Component //@Scope("prototype") public class Address implements Serializable { private static final long serialVersionUID = 1L; @Value("浙江") private String province; @Value("杭州") private String city; @Value("上城区") private String district; @Override public String toString() { return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + ", district='" + district + '\'' + '}'; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } }
配置文件:
<?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" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.itheima"></context:component-scan> </beans>
测试类(使用的spring整合的junit
pom中引入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> <scope>test</scope> </dependency>
):
package com.itheima.test; import com.itheima.domain.Address; import com.itheima.service.IUserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //junit中的注解,表示在SpringJUnit4ClassRunner中运行测试方法 @RunWith(SpringJUnit4ClassRunner.class) //ContextConfiguration指定配置文件,相对路径的applicationContext.xml @ContextConfiguration("classpath:applicationContext.xml") public class AnnotationTest1 { @Autowired//给对象赋值 private IUserService userService; @Test public void test1(){ userService.save(); } @Autowired private Address address; @Autowired private Address address1; @Test public void test2(){ System.out.println(address); System.out.println(address1); System.out.println(address==address1); } }
当运行test2时,结果:
address==address1,说明是同一个对象,当把scope注释打开时:
结果:
false说明对象不是同一个。
test1的相关代码:
IUserService:
package com.itheima.service; public interface IUserService { void save(); }
UserServiceImpl:
package com.itheima.service.impl; import com.itheima.dao.IUserDao; import com.itheima.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements IUserService { @Autowired private IUserDao userDao; public void save() { System.out.println(this.getClass().getName()+" 执行..."); userDao.save(); } }
IUserDao:
package com.itheima.dao; public interface IUserDao { void save(); }
UserDaoImpl:
package com.itheima.dao.impl; import com.itheima.dao.IUserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements IUserDao { @Override public void save() { System.out.println(this.getClass().getName()+" 执行..."); System.out.println("保存用户..."); } }
执行test1方法,结果:
个人简单理解,xml配置繁琐,配置跟代码分开,可读性差,但是成功解耦合。
注解开发简单,可读性强,但是注解写在了代码中,产生了耦合性,解耦合依赖面向切面编程。