[SPRING] autowire와 컴포넌트 주석을 사용할 때 Spring bean이 삽입되거나 생성되지 않습니다.
SPRINGautowire와 컴포넌트 주석을 사용할 때 Spring bean이 삽입되거나 생성되지 않습니다.
DI를 이해하려고 노력하고 있지만 autowire 및 구성 요소 주석을 사용하면 스프링 콩이 주입되거나 생성되지 않습니다. 대신 빈이 null이므로 nullpointer 예외가 발생합니다. 수동으로 생성해야합니까?이 경우 어디에서해야합니까? 여러 콩이 서로 의존 할 때 콩이 생성되는 순서는 어디에 지정합니까? 여기 내 코드가 있습니다 :
App.java
package se.springtest;
import se.springtest.Person;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class App
{
public static void main( String[] args )
{
BeanFactory factory = new XmlBeanFactory(
new ClassPathResource("application-context.xml"));
Person p = (Person) factory.getBean("person");
p.show();
}
}
application-context.xml
<?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="se.springtest"/>
</beans>
Person.java
package se.springtest;
public interface Person {
public void show();
}
PersonImpl.java
package se.springtest;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component("person")
public class PersonImpl implements Person {
private String firstName;
private String lastName;
private AdressInfo adress;
public PersonImpl() {firstName="Olle"; lastName="Olsson"; System.out.println("creating PersonImpl object");}
@Autowired
public void setAdress(AdressInfo adress) {
this.adress = adress;
}
public AdressInfo getAdress() {
return adress;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void show() {
System.out.println("Name is " + getFirstName() + " " +getLastName());
if (adress!=null)
adress.show();
else System.out.println("null");
}
}
AdressInfo.java
package se.springtest;
public interface AdressInfo {
public void show();
}
AdressInfoImpl.java
package se.springtest;
import org.springframework.stereotype.Service;
@Service("adress")
public class AdressInfoImpl implements AdressInfo {
private String adress;
public AdressInfoImpl() {adress="Storgatan 1"; System.out.println("creating AdressImpl object");}
public String getAdress() {
return adress;
}
public void setAdress(String adr) {
this.adress = adr;
}
public void show() {
System.out.println("Address is " + adress);
}
}
Pom.hml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.springtest</groupId>
<artifactId>spring-hello</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-hello</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>se.springtest.App</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC02</version>
</dependency>
</dependencies>
</project>
그리고 그것을 함께 컴파일합니다.
mvn clean compile exec:java
그러나 나는 얻는다.
Name is Olle Olsson
null
대신에
Name is Olle Olsson
Adress is Storgatan 1
누군가가 내게 어떤 문제인지 말할 수 있다면 정말 도움이 될 것입니다. 그것은 나와 어쩌면 다른 사람들이 DI를 더 잘 이해하게 만들 것입니다 ...
해결법
-
==============================
1.추가하려고 시도하십시오.
추가하려고 시도하십시오.
<context:annotation-config/>
봄 설정 파일.
-
==============================
2.BeanFactory는 단지 기능이 극히 제한적입니다. 콩을 인스턴스화하고 수동으로 함께 연결할 수 있도록 해줍니다. 찾고있는 기능은 Spring의 빵과 버터 인 ApplicationContexts에만 존재합니다. 변화
BeanFactory는 단지 기능이 극히 제한적입니다. 콩을 인스턴스화하고 수동으로 함께 연결할 수 있도록 해줍니다. 찾고있는 기능은 Spring의 빵과 버터 인 ApplicationContexts에만 존재합니다. 변화
BeanFactory factory = new XmlBeanFactory( new ClassPathResource("application-context.xml"));
에
BeanFactory factory = new ClassPathXmlApplicationContext( "application-context.xml");
그런 다음 "The BeanFactory"를 읽으면이 구별에 익숙해집니다.
from https://stackoverflow.com/questions/7182908/my-spring-bean-is-not-injected-or-created-when-i-use-the-autowire-and-component by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 세션 기반 데이터 소스가있는 스프링 부트 (0) | 2019.05.27 |
---|---|
[SPRING] Main 클래스에서 스프링 빈에 속성을 주입하는 방법 (0) | 2019.05.27 |
[SPRING] 속성을 찾을 수 없습니다 : 봄과 타일에 NoSuchAttributeException (0) | 2019.05.27 |
[SPRING] DTO 및 Entity 클래스 이름 지정 (0) | 2019.05.27 |
[SPRING] 런타임 생성자 인자를 가진 메소드에서 스프링 빈을 인스턴스화하는 방법? (0) | 2019.05.27 |