Spring框架学习第三讲(注解方式的bean装配)

注解方式的bean装配
一、注解的介绍
  1. 代码里面的特殊标记,使用注解可以完成功能;
  2. 注解写法:@注解名称(属性名称=属性值);
  3. 注解可以使用在类、属性和方法上面;
一、注解开发的准备
  1. 导入基本jar包+AOP的jar包
    beans,core,expression,context,log4j,commons-longging,aop

  2. 创建xml配置文件,且添加schema的约束,并且配置XML文件开启注解扫描;

  3. 创建对象的注解有四种,目前的功能相同,后续可能会有所扩展;

    • @Component
    • @Controller
    • @Service
    • @Reqository
  4. 创建对象单实例还是多实例

    • @Scope默认是单实例的;
    • @Scope(value=”prototype”)表示为多实例的

三、注解实现bean的装配(实例)
  1. 创建xml配置文件,且添加schema的约束,并且配置XML文件开启注解扫描;
<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"> <!-- bean definitions here -->
        <!-- 开启注解扫描,在包里面扫描类、方法、属性上面是否有注解 -->
    <context:component-scan base-package="com.spring.annotation"></context:component-scan>
</beans>
  1. 创建User类,并且添加注解
package com.spring.annotation;

import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Component(value="user")
public class User {
    public void add(){
        System.out.println("this is user add....."+name);
    }
}
  1. 写测试代码
package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.annotation.User;

public class AnnotationUser {
    @Test
    public void testAnnotation(){
        ApplicationContext context = new ClassPathXmlApplicationContext("annotationBean.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
        user.add();
    }
}
  1. 输出结果
com.spring.annotation.User@35a50a4c
this is user add.....null

四、注解注入属性
  1. 创建xml配置文件,且添加schema的约束,并且配置XML文件开启注解扫描,同上;

  2. 创建User类和UserService类,使用注解方式时,可以不创建setter方法,@AutoWired注解是根据对象类型自动注入的;@Resourse(name=”user”)注解必须添加name属性,而且是根据对象名称完成注入的。

User类
package com.spring.annotation;

import java.util.Properties;
import org.springframework.stereotype.Component;
@Component(value="user")
public class User {
    private String name="limi";

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

    public void add(){
        System.out.println("this is user add....."+name);
    }
}
UserService类
package com.spring.annotation;

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

@Component("userService")
public class UserService {
    //@Autowired   //用注解注入对象属性,根据对象类型查找
    @Resource(name="user12")   //根据对象名称查找
    private User user;

    public void print(){
        System.out.println("UserService print:"+user);
        user.add();
    }
}
  1. 写测试代码
@Test
public void testService(){
    ApplicationContext context = new ClassPathXmlApplicationContext("annotationBean.xml");
    UserService userService = (UserService) context.getBean("userService");
    System.out.println(userService);
    userService.print();
}
  1. 输出结果
com.spring.annotation.UserService@134593bf
UserService print:com.spring.annotation.User@4bb4de6a
this is user add.....limi
注解和配置文件混合使用
  1. 使用配置文件开启注解扫描,及对象bean的装配;
  2. 使用注解完成属性的注入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值