Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/guru/springframework/CustomBeanPostProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package guru.springframework;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
* Created by jt on 6/5/17.
*/
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

if(bean instanceof LifeCycleDemoBean){
((LifeCycleDemoBean) bean).beforeInit();
}

return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof LifeCycleDemoBean){
((LifeCycleDemoBean) bean).afterInit();
}

return bean;
}
}
14 changes: 13 additions & 1 deletion src/main/java/guru/springframework/DiDemoApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package guru.springframework;

import guru.springframework.controllers.ConstructorInjectedController;
import guru.springframework.controllers.GetterInjectedController;
import guru.springframework.controllers.MyController;
import guru.springframework.controllers.PropertyInjectedController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DiDemoApplication {

public static void main(String[] args) {
SpringApplication.run(DiDemoApplication.class, args);
ApplicationContext ctx = SpringApplication.run(DiDemoApplication.class, args);

MyController controller = (MyController) ctx.getBean("myController");

System.out.println(controller.hello());
System.out.println(ctx.getBean(PropertyInjectedController.class).sayHello());
System.out.println(ctx.getBean(GetterInjectedController.class).sayHello());
System.out.println(ctx.getBean(ConstructorInjectedController.class).sayHello());
}
}
69 changes: 69 additions & 0 deletions src/main/java/guru/springframework/LifeCycleDemoBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package guru.springframework;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
* Created by jt on 6/5/17.
*/
@Component
public class LifeCycleDemoBean implements InitializingBean, DisposableBean, BeanNameAware,
BeanFactoryAware, ApplicationContextAware{


public LifeCycleDemoBean() {
System.out.println("## I'm in the LifeCycleBean Constructor");
}

@Override
public void destroy() throws Exception {
System.out.println("## The Lifecycle bean has been terminated");

}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("## The LifeCycleBean has its properties set!");

}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("## Bean Factory has been set");
}

@Override
public void setBeanName(String name) {
System.out.println("## My Bean Name is: " + name);

}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("## Application context has been set");
}

@PostConstruct
public void postConstruct(){
System.out.println("## The Post Construct annotated method has been called");
}

@PreDestroy
public void preDestroy() {
System.out.println("## The Predestroy annotated method has been called");
}

public void beforeInit(){
System.out.println("## - Before Init - Called by Bean Post Processor");
}

public void afterInit(){
System.out.println("## - After init called by Bean Post Processor");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

/**
* Created by jt on 5/24/17.
*/
@Controller
public class ConstructorInjectedController {

private GreetingService greetingService;

public ConstructorInjectedController(@Qualifier("constructorGreetingService") GreetingService greetingService) {
this.greetingService = greetingService;
}

public String sayHello(){
return greetingService.sayGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

/**
* Created by jt on 5/24/17.
*/
@Controller
public class GetterInjectedController {
private GreetingService greetingService;

public String sayHello(){
return greetingService.sayGreeting();
}

@Autowired
public void setGreetingService(@Qualifier("getterGreetingService") GreetingService greetingService) {
this.greetingService = greetingService;
}
}
23 changes: 23 additions & 0 deletions src/main/java/guru/springframework/controllers/MyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingService;
import org.springframework.stereotype.Controller;

/**
* Created by jt on 5/23/17.
*/
@Controller
public class MyController {

private GreetingService greetingService;

public MyController(GreetingService greetingService) {
this.greetingService = greetingService;
}

public String hello(){
System.out.println("Hello!!! ");

return greetingService.sayGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.controllers;

import guru.springframework.services.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;


/**
* Created by jt on 5/24/17.
*/
@Controller
public class PropertyInjectedController {

@Autowired
@Qualifier("greetingServiceImpl")
public GreetingService greetingServiceImpl;

public String sayHello(){
return greetingServiceImpl.sayGreeting();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package guru.springframework.services;

import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
public class ConstructorGreetingService implements GreetingService {
@Override
public String sayGreeting() {
return "Hello - I was injected via the constructor!!!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package guru.springframework.services;

import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
public class GetterGreetingService implements GreetingService {

@Override
public String sayGreeting() {
return "Hello - I was injected by the getter";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package guru.springframework.services;

/**
* Created by jt on 5/24/17.
*/
public interface GreetingRepository {

String getEnglishGreeting();

String getSpanishGreeting();

String getGermanGreeting();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package guru.springframework.services;

import org.springframework.stereotype.Component;

/**
* Created by jt on 5/24/17.
*/
@Component
public class GreetingRepositoryImpl implements GreetingRepository {

@Override
public String getEnglishGreeting() {
return "Hello - Primary Greeting service";
}

@Override
public String getSpanishGreeting() {
return "Servicio de Saludo Primario";
}

@Override
public String getGermanGreeting() {

return "Primärer Grußdienst";

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package guru.springframework.services;

/**
* Created by jt on 5/24/17.
*/
public interface GreetingService {

String sayGreeting();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package guru.springframework.services;

import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
public class GreetingServiceImpl implements GreetingService {

public static final String HELLO_GURUS = "Hello Gurus!!!! - Original";

@Override
public String sayGreeting() {
return HELLO_GURUS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guru.springframework.services;

import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
@Primary
@Profile("de")
public class PrimaryGermanGreetingService implements GreetingService {

private GreetingRepository greetingRepository;

public PrimaryGermanGreetingService(GreetingRepository greetingRepository) {
this.greetingRepository = greetingRepository;
}

@Override
public String sayGreeting() {
return greetingRepository.getGermanGreeting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guru.springframework.services;

import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

/**
* Created by jt on 5/24/17.
*/
@Service
@Primary
@Profile({"en", "default"})
public class PrimaryGreetingService implements GreetingService {

private GreetingRepository greetingRepository;

public PrimaryGreetingService(GreetingRepository greetingRepository) {
this.greetingRepository = greetingRepository;
}

@Override
public String sayGreeting() {
return greetingRepository.getEnglishGreeting();
}
}
Loading