• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
土上方方
博客园    首页    新随笔    联系   管理    订阅  订阅

AOP注解

Ioc和Aop使用扩展

DI注入(设置注入/构造注入/p命名注入)

1.构造注入

第一步(类1)

 

 

(类二)

第二步:Xml文件

 

第三步:单测

 

 

 

3.(DI)注解 1.(小案例)

第一步:xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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"
>
  <context:component-scan base-package="cn.zhujiDI"/>
</beans>

第二步:实体类(Student类)

 

package cn.zhujiDI;


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

import javax.annotation.Resource;

/**
 * Student
 */
@Component("student")
public class Student {
   @Value("阳光")
    private String name;
   @Value("18")
    private int age;

    public Car getCar() {
        return car2;
    }

    public void setCar(Car car) {
        this.car2 = car2;
    }

    @Resource()
  private  Car car2;
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car2 +
                '}';
    }
}
posted @ 2017-07-29 11:18  土上方方  阅读(209)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3