Spring Bean中怎么对注解进行管理(bean,spring,编程语言)

时间:2024-05-06 02:28:00 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

1.使用注解的方式需要配置applicationContext.xml:

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:component-scanbase-package="org.yzytest1"></context:component-scan><!--开启包扫描--></beans>

2.将类交给Spring管理:

@Component("Demo1")//使用注解ComponentpublicclassDemo1{@Value("yzy")privateStringname;publicvoidsay(){System.out.println("你好呀!"+name);}}

Spring Bean中怎么对注解进行管理

3.Spring的属性注入:

普通的属性注入,使用@Value属性注入:

@Component("Demo1")publicclassDemo1{@Value("yzy")//使用注解Value,属性注入privateStringname;publicvoidsay(){System.out.println("你好呀!"+name);}}

复杂的属性注入,使用@Resource属性注入:

importorg.springframework.stereotype.Component;importjavax.annotation.Resource;@Component("Demo1")publicclassDemo1{@Resource(name="User")//使用@Resource,属性注入对象privateUseruser;publicvoidsay(){System.out.println("你好呀!"+user.getUsername());}}

4.Spring的其他注解:

Spring Bean中怎么对注解进行管理

Spring Bean中怎么对注解进行管理

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Spring Bean中怎么对注解进行管理的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java后台判断ajax请求及处理过程详解下一篇:

3 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18