zoukankan      html  css  js  c++  java
  • @ 添加属性(属性注入)

    //配置

    <?xml version="1.0" encoding="UTF-8"?>
    <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/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "
    xmlns="http://www.springframework.org/schema/beans">
    <context:component-scan base-package="cn.jy.domain"></context:component-scan>
    </beans>

    //User类

    package cn.lijun.domain;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;

    //<bean name="user" class="cn.lijun.domain.User">
    //@Component("user")
    //@Service("user")
    //@Controller("user")
    @Repository("user")
    //@Scope(scopeName="singleton")
    public class User {
        private String name;
        
        private Integer age;
        @Autowired
        @Qualifier("car")
        private Car car;
        
        public User() {
            
        }  
     public User(String name, Integer age, Car car) {
            this.name = name;
            this.age = age;
            this.car = car;
        }
    public User(Integer age, Car car) {
            this.age = age;
            this.car = car;
        }
        public String getName() {
            return name;
        }
        @Value("QIANG")
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        @Value("22")
        public void setAge(Integer age) {
            this.age = age;
        }
        public Car getCar() {
            return car;
        }
        public void setCar(Car car) {
            this.car = car;
        }
    //    @PostConstruct
    //    public void init(){
    //        System.out.println("我是初始化方法");
    //    }
    //    @PreDestroy
    //    public void destory(){
    //        System.out.println("我是销毁方法");
    //    }
        @Override
        public String toString() {
            return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
        }
        
    }

    //Car类

    package cn.jy.domain;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    @Component("car")
    public class Car {
         private String name;
         private String color;

    public String getName() {
            return name;
        }
        @Value("法拉利")

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

    public String getColor() {
            return color;
        }
        @Value("呕吐绿" + "")

    public void setColor(String color) {
            this.color = color;
        }
        @Override
    public String toString() {
            return "Car [name=" + name + ", color=" + color + "]";
        }
         
    }

  • 相关阅读:
    读书笔记_Effective_C++_条款十:令operator=返回一个reference to *this
    读书笔记_Effective_C++_条款九:绝不在构造和析构函数中调用virtual函数
    读书笔记_Effective_C++_条款七:为多态基类声明virtual析构函数
    读书笔记_Effective_C++_条款十二:复制对象时勿忘其每一个成分
    读书笔记_Effective_C++_条款八:别让异常逃离析构函数
    创建django project及app中的若干问题
    python一些小知识
    python小专题——JSON
    python小专题——optparse模块
    初窥JQuery(一)选择符 【转】
  • 原文地址:https://www.cnblogs.com/Fisherman13/p/10555134.html
Copyright © 2011-2022 走看看