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 + "]";
        }
         
    }

  • 相关阅读:
    谈URL中末尾斜杠对SEO的影响
    ORDER BY一个较高级的用法
    MYSQL5.5 提示 Mysq error:Cannot load from mysql.proc
    mysql 数据库信息泄露
    [转]PclZip简介与使用
    通过telnet命令查看memcache运行状态
    [转载]PHP上传问题总结(文件大小检测,大文件上传)
    Silex 基于Symfony2组件的微型框架
    [转]推荐一些不错的计算机书籍
    [转]Beanstalkd简介(job生命周期)
  • 原文地址:https://www.cnblogs.com/Fisherman13/p/10555134.html
Copyright © 2011-2022 走看看