首页 > 编程语言 > 详细

Spring 注解配置进阶

时间:2021-03-28 13:02:54      阅读:21      评论:0      收藏:0      [点我收藏+]

@Component

在xml文件上加这句代码

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!--  配置注解要扫描的包  -->
    <context:component-scan base-package="Demo"/>
    <context:annotation-config/>
</beans>

在类的定义时

package Demo.dao;

import org.springframework.stereotype.Component;

@Component
//等价于 <bean id="user" class="Demo.dao.User"/>
public class User {
    private String name;
}

此时该类就已经配置好了
可以通过@Value赋值

package Demo.dao;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
//等价于 <bean id="user" class="Demo.dao.User"/>
public class User {
    @Value("jie")
    //赋值
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

还有很多其他方法 不细说

重点在于然后去均衡xml配置和注解配置
技术分享图片

Spring 注解配置进阶

原文:https://www.cnblogs.com/OfflineBoy/p/14588145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!