Spring入门详细教程(二)

前端之家收集整理的这篇文章主要介绍了Spring入门详细教程(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇。链接如下:

Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/10165538.html

一、spring注入方式

1、set方法注入

  1. <bean name="user" class="com.jichi.entity.User" >
  2.   <property name="name" value="小明"></property>
  3.   <property name="age" value="18"></property>
  4. </bean>

2、构造方法注入

  1. <bean name="user" class="com.jichi.entity.User" >
  2. <constructor-arg name="name" value="小红" ></constructor-arg>
  3.   <constructor-arg name="age" value="50"></constructor-arg>
  4. </bean>

3、p名称空间注入

  1. xmlns:p="http://www.springframework.org/schema/p"
  1. <bean name="user" class="com.jichi.entity.User" p:name="小白" p:age="10"></bean>

4、spel表达式注入

  1. <bean name="user" class="com.jichi.entity.User">
  2. <property name="name" value="小红"></property>
  3. <property name="age" value="18"></property>
  4. </bean>
  5. <bean name="user1" class="com.jichi.entity.User">
  6. <property name="name" value="#{user.name}"></property>
  7. <property name="age" value="#{user.age}"></property>
  8. </bean>

二、spring复杂类型注入

  1. public class Collection {
  2. public String[] arr;
  3. public List<String> list;
  4. public Map<String,Object> map;
  5. Properties props;
  6. String[] getArr() {
  7. return arr;
  8. }
  9. void setArr(String[] arr) {
  10. this.arr = getList() {
  11. list;
  12. }
  13. void setList(List<String> list) {
  14. this.list = getMap() {
  15. map;
  16. }
  17. void setMap(Map<String,1)"> map) {
  18. this.map = Properties getProps() {
  19. props;
  20. }
  21. setProps(Properties props) {
  22. this.props = props;
  23. }
  24. @Override
  25. String toString() {
  26. return "Collection [arr=" + Arrays.toString(arr) + ",list=" + list + ",map=" + map + ",props=" + props + "]";
  27. }
  28. }

1、数组类型注入

  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="arr">
  3. <array>
  4. <value>xiaohei</value>
  5. <value>xiaobai</value>
  6. </array>
  7. </property>
  8. </bean>

2、list类型注入

  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="list">
  3. <list>
  4. <value>xiaohei</value>
  5. <value>xiaobai</value>
  6. </list>
  7. </property>
  8. </bean>

3、map类型注入

  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="map">
  3. <map>
  4. <entry key="name" value="xiaohei"></entry>
  5. <entry key="age" value="18"></entry>
  6. </map>
  7. </property>
  8. </bean>

4、properties类型注入

  1. <bean name="collect" class="com.jichi.entity.Collection">
  2. <property name="props">
  3. <props>
  4. <prop key="name">xiaohei</prop>
  5. <prop key="age">18</prop>
  6. </props>
  7. </property>
  8. </bean>

三、配置spring随web项目启动初始化

在web.xml中配置。

  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:applicationContext.xml</param-value>
  4. </context-param>

四、spring的分配置文件

方式一:

  1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml")

方式二:

  1. <import resource="applicationContext.xml"></import>

五、spring注解配置

1、开启注解扫描

  1. <context:component-scan base-package="com.jichi.entity"></context:component-scan>

扫描com.jichi.entity下的所有类中的注解。

2、在类上添加注解

  1. @Component
  2. User {
  3. }

六、spring常用注解

1、@Componet,@Controller,@Service,@Repository四个组件注解,作用在类上。四个注解并无区别,只是为了方便区分。

2、@Scope注解,作用在类上。

  1. @Scope(scopeName="singleton") //单例模式
  2. User {
  3. }
  1. @Scope(scopeName="prototype") //多例模式
  2. User {
  3. }

3、@Value用于注入普通类型值

第一种方式:作用在属性上,通过反射的filed值,破坏了对象的封装性。

  1. @Value("xiaohei")
  2. private String name;

第二种方式:通过set方法赋值,不破坏对象的封装性。

  1. @Value("xiaobai")
  2. setName(String name) {
  3. this.name = name;
  4. }

4、@Autowired,@Resource,@Qualifier注解 

引用类型的装配方式,详细区别请看之前的博客

  1. @Autowired
  2. private Car car;
  1. @Resource
  2. private Car car;

5、@PostConstruct与@PreDestroy

  1. @PostConstruct //创建对象前调用
  2. init(){
  3. System.out.println("初始");
  4. }
  5. @PreDestroy   //对象销毁调用
  6. destory(){
  7. System.out.println("销毁");
  8. }

七、spring与junit整合测试

1、导入spring基础包,与aop包和test包,可从lib中找到。

2、在测试类上添加注解

  1. @RunWith(SpringJUnit4ClassRunner.)
  2. @ContextConfiguration("classpath:applicationContext.xml" TestJunit {
  3. @Resource
  4. private User user;
  5. @Test
  6. test1(){
  7. System.out.println(user);
  8. }
  9. }

 

猜你在找的Spring相关文章