Spring学习4

一、使用注解开发

  • 在Spring4之后,要使用注解开发,必须要保证aop的包已经导入了

 

  • 使用注解需要导入context约束,增加注解的支持
<?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.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

</beans>

属性如何注入

//等价于:<bean id="user" class="com.rxj.pojo.User">
//@Component    组件,放在类上,说明这个类被Spring管理了,就是bean
@Component
public class User {
    //相当于<property name="name" value="来一沓Java"/>
    //@Value("xxx") 放在属性上面,可以指定属性的值
    @Value( "来一沓Java" )
    public String name;
}

 衍生的注解

        @Component衍生的注解,在Web开发中,会按照mvc三层架构分层

  • dao层           【@Repository】
  • service层      【@Service】
  • controller层   【@Controller】

这四个注解功能都是一样的,都代表将某个类注册到Spring中,装配Bean

二、xml与注解相比

  • xml更加万能,适用于任何场景!维护简单方便
  • 注解不是自己的类使用不了,维护相对复杂

三、xml与注解最佳实践

  • xml用来管理bean
  • 注解只负责完成属性的注入
  • 在使用过程中,只需要让注解生效,就需要开启注解的支持
<!--    指定要扫描的包,指定的这个包下的注解就会生效-->
    <context:component-scan base-package="com.rxj"/>
    <context:annotation-config/>

 四、使用java的方式配置Spring

在这里我们完全不使用Spring的xml配置了,全权交给Java来做。

javaConfig早期是Spring的一个子项目,在Spring4之后,它变成了核心功能!

实体类:

//这个注解的意思是说这个类被Spring接管了,注册到了容器中
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value( "来一沓Java" ) //注入属性的值
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

配置类一:

@Configuration //这个也会被Spring容器托管,注册到容器中,它本身就是一个@Component
// @Configuration代表这个是一个配置类,就和applicationContext.xml是一样的
@ComponentScan("com.rxj.pojo")//扫描包
@Import( UserConfig2.class )//导入其他的配置类
public class UserConfig {
    //注册一个bean,相当于xml中的一个bean
    //这个方法的名字,相当于bean标签中id的属性值
    //这个方法的返回值,相当于bean标签中class的属性值
    @Bean
    public User user(){
        return new User();//返回要注入到bean的对象
    }
}

配置类二:

@Configuration
public class UserConfig2 {
}

测试类:

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用配置类去做,就只能通过AnnotationConfigApplicationContext上下文获取容器,通过配置类的class对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext( UserConfig.class );
        User user = (User) context.getBean( "user" );
        System.out.println( user.getName() );
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来一沓Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值