spring-使用注解开发

自动装配的使用

1.配置文件注入bean


	<bean id="cat" class="Bean.Cat"></bean>

//scope 设置bean的作用域,singleton 表示单例模式,只有一个bean
    <bean id="dog" class="Bean.Dog" scope="singleton"></bean>
 <bean id="dog2" class="Bean.Dog" scope="singleton"></bean>
//byName 仅根据id的name来装配,byType,仅根据类路径来装配,可以么有id
    <bean id="people" class="Bean.People" autowire="byName"></bean>
可以通过注解@Autowired,放在属性上来实现自动注解,
    
   //如果Autowired 定义了required属性为false,说明这个对象可以为null,否则不允许
    @Autowired(required = false)
    //有相同的name
    @Qualifier(value="cat2")
    private  Dog dog;
    @Resource(name = "cat")
    private Cat cat;

2.注解自动生成bean
注意:
1.导入注解的约束:context

2.配置注解的支持: context:annotation-config/

<!--配置文件中添加以下内容,-->
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
//扫描以下包com.kuang.pojo
<context:component-scan base-package="com.kuang.pojo"/>
    //引用注解必须有
<context:annotation-config/>  
</beans>

3.在需要生成bean的java类上添加注解compontent

package com.kuang.pojo;

import org.springframework.stereotype.Component;

@Component //在扫描包的时候,遇到这样的注解,则为bean
public class User {
    public void  showName(){
        System.out.println("我的名字时哈哈");
    }
}

==============================================

1.bean

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

在这里插入图片描述
在使用注解开发,必须添加coentext约束,添加注解支持

<!--配置文件中添加以下内容,-->
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
//扫描以下包com.kuang.pojo
<context:component-scan base-package="com.kuang.pojo"/>
    //引用注解必须有
    <context:annotation-config/>  

</beans>

在扫描包的java类上加上@Component注解,表示这个java类作为一个bean被spring管理,

2.属性的注入

可以在属性上添加,也可以在set方法上添加

添加了
@Component
public class User {
 @Value("狂神")
 public String name ;
 public int age;
 public void setName(String name) {
  this.name = name;
 }
 //相当于<property name="age" value = "12"/>
@Value("12")
 public void setAge(int age) {
  this.age = age;
 }
@Component
public class User {
    @Value("名字")
    public String name;
    public void  showName(){
        System.out.println("我的名字时哈哈");
    }
    public String getName() {
        return name;
    }
}

3.衍生注解

@Component有几个衍生注解,作用都是将修饰的类注册到容器中交给spring管理,安装mvc的架构区分
。Dao @Repository 主要用于和数据库交互
。service @Service 用户修饰业务层
。Controller @Controller 用来修饰控制器层

4.自动装配

@Autowired :自动装配通过类型,或者名字
 如果Autowired不能唯一自动装配,多个类型不同名称的bean,则需要通过@Qualifier(value="xxx")配合使用。
@Nullable 标记了这个字段,说明这个字段可以为null
@Resource :类似于@Autowired,这是一个java的注解,不属于spring里面的注解。默认byName,负责不要Type,依然没有则报错

5作用域

@Component
@Scope("prototype")//作用域设置
public class User {
 @Value("狂神")
 public String name ;
 public int age;
 public void setName(String name) {
  this.name = name;
 }
 //相当于<property name="age" value = "12"/>
@Value("12")
 public void setAge(int age) {
  this.age = age;
 }
}

6小结

xml配置和注解配置

1.xml配置更加万能,适合任何场景

2.注解,不是自己的类无法使用

3.最佳使用组合,xml配置用来管理bean,注解的方式用来添加属性。

=========================================
纯java类实现,不需要配置文件
bean对象

@Scope("sigleType")//是否单例模式设置
@Component//这个注解还是表示这个类被spring管理了
public class User {
    @Value("胡歌")
    private String name;

    public String getName() {
        return name;
    }

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

配置类

@Configuration//表示这是一个配置类,类似于beans.xml。同时这个类也是被spring管理了。
@ComponentScan("com.kuang")//扫描包用,
@Import(AppConfig2.class)//如果有多个配置类,可以通过这个方式记载到一个类里面
public class AppConfig {

    //注册一个bean,相当于写的bean标签的作用
    //这个方法的名字相当于id,
    // 返回值相当于class属性
    @Bean
    public User getUser(){
        return  new User();
    }
}

测试类

 public static void main(String[] args) {
        //完全用java类实现,获取bean的方式时AnnotationConfig,上下文容器通过配置了的class对象加载
     ApplicationContext context =   new AnnotationConfigApplicationContext(AppConfig.class);
    User user =(User) context.getBean("getUser");
System.out.println(user.getName());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值