Spring Annotation无效@Autowired

时间:2017-08-17 20:08:59

标签: java spring

我试图了解DI,但是当我使用autowire时,我的春豆没有注入或创建。我是春天的新手,这是我的第一个注释应用程序。     我正在使用JRE 7.我想打印@Autowired实例变量的详细信息。但是,虽然我已经提供了值为pune的null值。请帮我。它不是打印pune而是打印null。

<!-- lang: java -->

    package com.spring;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;


    public class Employee {
        //@Value("Subhash")
        private String name;
        private String role;
        @Autowired
        private Address address;

        public Employee() {
            // TODO Auto-generated constructor stub
        }
        public Employee(String name, String role, Address address) {
            super();
            this.name = name;
            this.role = role;
            this.address = address;
        }

        public String getName() {
            return name;
        }
        public String getRole() {
            return role;
        }
        public Address getAddress() {
            return address;
        }
        public void setName(String name) {
            this.name = name;
        }
        public void setRole(String role) {
            this.role = role;
        }

        public void setAddress(Address address) {
            this.address = address;
        }
    }

My main method

   package com.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientLogic {
    public static void main(String arg[]){
        //Resource r = new ClassPathResource("config.xml");
        //BeanFactory b = new XmlBeanFactory(r);

        ApplicationContext ap = new ClassPathXmlApplicationContext("config.xml");
        Object object = ap.getBean("employee");
        Employee w = (Employee) object;

        System.out.println(w.getName()+w.getAddress().getCity());


    }
}


The other POJO

    package com.spring;

    public class Address {

        private String city;
        private String pinCode;

        public Address(String city, String pinCode) {
            super();
            this.city = city;
            this.pinCode = pinCode;
        }
        public String getCity() {
            return city;
        }
        public String getPinCode() {
            return pinCode;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public void setPinCode(String pinCode) {
            this.pinCode = pinCode;
        }
    }

And my XML configuration

    <?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:jdbc="http://www.springframework.org/schema/jdbc"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.spring"/>
    <bean id="welcome" class="com.spring.Welcome"></bean>
    <bean id="employee" class="com.spring.Employee">
        <property name="role" value="45"></property>
    </bean>
    <bean id="address" class="com.spring.Address">
        <constructor-arg value="pune"></constructor-arg>
        <constructor-arg value="411057"></constructor-arg>
    </bean>
</beans>

0 个答案:

没有答案