Commit 8f4c5995 authored by dbhuller's avatar dbhuller

autowired beans at class level with steretype annotations

parent 7f21e077
......@@ -4,26 +4,29 @@ import com.example.service.SpeakerService;
import com.example.service.SpeakerServiceImpl;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
@ComponentScan({"com.example"})
public class AppConfig {
/*
@Bean(name = "speakerService")
@Scope(value = BeanDefinition.SCOPE_SINGLETON)
// @Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public SpeakerService getSpeakerService() {
// Using Constructor Injection
//Using Constructor Injection
SpeakerServiceImpl service = new SpeakerServiceImpl(getSpeakerRespository());
// config service with repository (setter injection)
// service.setRepository(getSpeakerRespository());
//config service with repository (setter injection)
service.setRepository(getSpeakerRespository());
SpeakerServiceImpl service = new SpeakerServiceImpl();
return service;
}
@Bean(name = "speakerRepository")
public SpeakerRepository getSpeakerRespository() {
return new HibernateSpeakerRepositoryImpl();
}
}*/
}
package com.example.repository;
import com.example.model.Speaker;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository("speakerRepository")
public class HibernateSpeakerRepositoryImpl implements SpeakerRepository {
@Override
......
......@@ -3,14 +3,23 @@ package com.example.service;
import com.example.model.Speaker;
import com.example.repository.HibernateSpeakerRepositoryImpl;
import com.example.repository.SpeakerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("speakerService")
public class SpeakerServiceImpl implements SpeakerService {
private SpeakerRepository repository;
public SpeakerServiceImpl() {
System.out.println("SpeakerServiceImpl no args Constructor");
}
public SpeakerServiceImpl(SpeakerRepository repository) {
System.out.println("SpeakerServiceImpl repository Constructor");
this.repository = repository;
}
......@@ -19,7 +28,9 @@ public class SpeakerServiceImpl implements SpeakerService {
return repository.findAll();
}
@Autowired
public void setRepository(SpeakerRepository repository) {
System.out.println("SpeakerServiceImpl repository setter");
this.repository = repository;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment