5.Spring MVC 自动装配问题

一.使用@controller注解,实际上也是在IOC容器中配置了,它的id是类的首字母小写

一.使用@controller注解,实际上也是在IOC容器中配置了,它的id是类的首字母小写

1.如果不使用注解,在IOC容器中通过配置来加载bean。

<?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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!--加载bean-->
    <bean id="userController" class="com.neuedu.controller.UserController"></bean>

</beans>

如果使用注解的方式,在配置文件中要扫描包

<?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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  <!-- 扫描包-->
    <context:component-scan base-package="com.neuedu"></context:component-scan>  
</beans>

写一个controller层

ackage com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.neuedu.service.UserService;

@Controllerpublic class UserController {
  public void sayHello(){
        
       System.out.println("say Hello");
    }

}

使用@controller注解,实际上也是在IOC容器中配置了,它的id是类的首字母小写

可以写一个Junit test case

public class TestIOC {
    private ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void test() {
//使用UserController类的id来调用
        Object bean = ioc.getBean("userController");
        System.out.println(bean);
    }

}

@controller也可以更改id,这个注解有一个value属性值

比如:@Controller(value=" zhangsan")

package com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.neuedu.service.UserService;

@Controller(vaqlue="zhangsan")
public class UserController {
  
    public void sayHello(){        
       System.out.println("say Hello");
    }
}

在Junit test  case中

public class TestIOC {
    private ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void test() {
//使用UserController类的id来调用
        Object bean = ioc.getBean("zhangsan");
        System.out.println(bean);
    }
}

@Autowired标签

@Controller(value="zhangsan")
public class UserController {
    @Autowired
    private UserService userService;
    public void sayHello(){        
        userService.sayHello();    }

}

Autowired标签

  1.首先是将使用  UserService 类,

  2.如果类有冲突就使用 id,其实就属性名userService

1]首先检测标记了@Autowired注解的属性的类型
[2]根据类型进行装配
[3]如果指定类型的bean不止一个,那么根据需要被装配的属性的属性名做id的值,查找bean
[4]如果根据id值还是没有找到bean,可以使用@Qualifier注解手动指定要装配的bean的id.

原文地址:https://www.cnblogs.com/lukelook/p/9635008.html