Spring 注解开发

梦里梦外; 2022-10-08 00:45 299阅读 0赞

1、@Component组件

第一步:指定扫描的包,这个包下的注解就会生效

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. https://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--指定扫描的包,这个包下的注解就会生效-->
  10. <context:component-scan base-package="com.lxc.domain" />
  11. <context:annotation-config/> <!--注解的支持-->
  12. <bean id="user" class="com.lxc.domain.User"/>
  13. </beans>

第二步:添加 @Component 注解

  1. package com.lxc.domain;
  2. import org.springframework.stereotype.Component;
  3. //@Component 组件 【等于】 <bean id="user" class="com.lxc.domain.User"/>
  4. @Component
  5. public class User {
  6. public String name = "123";
  7. }

第三部:测试

  1. public class Test {
  2. public static void main(String[] args) {
  3. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans2.xml");
  4. User user = (User) ctx.getBean("user");
  5. System.out.println(user.name);
  6. }
  7. }

20210619094342876.png

属性如何注入​​​​​​​ @Value(“value”)

  1. package com.lxc.domain;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. @Component 组件 【等于】 <bean id="user" class="com.lxc.domain.User"/>
  6. @Value("name值~~~") 【等于】
  7. <bean id="user" class="com.lxc.domain.User">
  8. <property name="name" value="name值~~~"></property>
  9. </bean>
  10. */
  11. @Component
  12. public class User {
  13. @Value("name值~~~")
  14. public String name;
  15. }

2、衍生注解

@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层

dao 层【@Repository】
20210619100217952.png

service层 【@Service】
20210619100204573.png

controller层 【Controller】
20210619100238157.png

这是个注解功能都是一样的,都是将某个类注册到spring中,装配Bean。

3、@scope

作用域

  1. /**
  2. @Scope("singleton") 等于 <bean id="user" class="" scope="singleton" />
  3. */
  4. @Scope("singleton")
  5. public class User {
  6. @Value("name值~~~")
  7. public String name;
  8. }

总结:

(1)xml更加万能,适用于任何场景!维护简单。
(2)注解开发,维护起来相对复杂。
(3)一般开发中,xml来管理bean,注解实现属性的注入。
(4)使用直接之前一定要开启注解支持

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. https://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--指定扫描的包,这个包下的注解就会生效-->
  10. <context:component-scan base-package="com.lxc.domain" />
  11. <!--注解的支持-->
  12. <context:annotation-config/>
  13. </beans>

发表评论

表情:
评论列表 (有 0 条评论,299人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring 注解开发

    一、spring的xml配置开发与注解开发的对应关系 自从2.5版本开始,Spring就支持了注解开发。一直到3.0支持纯注解开发。那么注解开发与原来的配置开发有什么关系

    相关 spring 注解开发

    spring 注解开发 注解:为了代替配置文件,让配置信息和java代码看起来更直观 (注,JDK1.8和spring4.0以上才兼容) 也起解释说明和检查的作用,