SpringMVC使用注解配置bean

本文介绍了在Spring框架中如何通过XML配置文件和注解两种方式来定义和管理bean。XML配置方式直接在配置文件中声明bean,而注解方式则通过@Component等注解在类上标记并可通过<context:component-scan>扫描特定包来自动注册bean。此外,还讨论了@Autowired注解的使用及工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果不使用注解,在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层

package 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/xuesheng/p/7445264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值