siemacie
Natkneło mnie jakoś żeby popróbować konfiguracji springa przez klase, nie xml'a, wiec zrobilem projekt, jakas tam podstawową konfiguracje i próbuje wstrzykiwać klasy przez @Autowired i coś mi nie bangla bo wyrzuca NullPointerException. Przekopałem troche internety i gdzieś musze robić głupi błąd, może wy wiecie o co chodzi?
Klasa konfiguracyjna: (ten bean person to tak dla sprawdzenia wczesniej robiłem)
import com.maciej.entities.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class ConfigClass {
@Bean
public Person person(){
return new Person();
}
}
PersonService:
import org.springframework.stereotype.Service;
@Service
public class PersonService {
public void create(){
System.out.println("Person created.");
}
public void delete(){
System.out.println("Person deleted.");
}
}
Klasa testowa:
import com.maciej.ConfigClass;
import com.maciej.entities.Person;
import com.maciej.services.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Test {
@Autowired
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public static void main(String[] args){
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(ConfigClass.class);
Person person = acac.getBean("person", Person.class);
person.setId(1L);
person.setName("Wojciech");
System.out.println(person.toString());
new Test().getPersonService().create(); <<<<<------------ ta linijka wyrzuca NullPointera
}
}
i może jeszcze POM gdyby to miało znaczenie:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maciej</groupId>
<artifactId>com.maciej</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.maciej</name>
<a href="http://maven.apache.org">http://maven.apache.org</a>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Przy okazji:
Czy teraz jest możliwość 100%towej konfiguracji springa za pomocą klas? Są adnotacje ekwiwalentne do np. context:annotation-config/ albo mvc:annotation-driven/?
Tak samo web.xml -> gdzieś słyszałem, że jego też można już za pomocą klas konfigurować :P
A ogólnie często się spotykacie z takimi klasami, czy raczej pozostaje się wciąż w standardowych xmlach?