Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
76071d5
adding context example
springframeworkguru May 23, 2017
b2cac1c
adding Manual DI Example
springframeworkguru May 24, 2017
32c671f
making controller greeting methods public
springframeworkguru May 24, 2017
29ca0a5
making controller greeting methods public
springframeworkguru May 24, 2017
69f7c68
adding more services for Qualifier example
springframeworkguru May 24, 2017
d96580a
removing unused imports
springframeworkguru May 24, 2017
fc7db2a
adding Primary Bean demo
springframeworkguru May 24, 2017
bbb782f
adding Primary Bean demo
springframeworkguru May 24, 2017
f84eaea
adding default profile demo
springframeworkguru May 24, 2017
b22f58e
adding default profile demo
springframeworkguru May 24, 2017
4e84b2d
adding default profile demo
springframeworkguru May 24, 2017
c846e98
adding default profile demo
springframeworkguru May 25, 2017
63dcb40
adding factory bean example
springframeworkguru May 25, 2017
04659e1
adding property source example
springframeworkguru Jun 7, 2017
75c0411
adding environment example
springframeworkguru Jun 7, 2017
26dd4db
adding multiple property files
springframeworkguru Jun 7, 2017
c8d6371
updating Spring Boot version
springframeworkguru Jun 19, 2017
90d566f
Updating Spring Boot Version
springframeworkguru Jul 31, 2017
3632f75
Updated to 2.0.0.M7
springframeworkguru Dec 10, 2017
13971ef
Updated to Spring Boot 2.0.0.RELEASE
springframeworkguru Mar 18, 2018
8a2dc2b
Upgraded to Spring Boot 2.1.0.RELEASE
springframeworkguru Dec 6, 2018
80eea99
25/10/2021 added some comments
seydaozdmr Oct 25, 2021
ed0981f
25/10/2021 setter injected controller
seydaozdmr Oct 25, 2021
68c4cfc
25/10/2021 GreetingService Factory Example
seydaozdmr Oct 25, 2021
f0b14c3
25/10/2021 Greeting Service @Qualifier example
seydaozdmr Oct 25, 2021
f68cd47
25/10/2021 SetterInjection
seydaozdmr Oct 25, 2021
fa4985b
25/10/2021 some mistakes amended
seydaozdmr Oct 25, 2021
8fc3449
27/10/2021 changed with repo
seydaozdmr Oct 27, 2021
befe572
27/10/2021 test
seydaozdmr Oct 27, 2021
8594383
27/10/2021 repo package created
seydaozdmr Oct 27, 2021
5b9e67f
27/10/2021 basePrimaryGreetingService bean is created
seydaozdmr Oct 27, 2021
e4c521b
27/10/2021 package changes
seydaozdmr Oct 27, 2021
066f760
27/10/2021 source
seydaozdmr Oct 27, 2021
8af4f50
28/10/2021 scope
seydaozdmr Oct 28, 2021
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
40 changes: 2 additions & 38 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<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>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>11</java.version>
</properties>

<dependencies>
Expand All @@ -46,43 +46,7 @@
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>


</project>
49 changes: 48 additions & 1 deletion src/main/java/guru/springframework/DiDemoApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
package guru.springframework;

import guru.springframework.controllers.GreetingFactoryController;
import guru.springframework.controllers.MyController;
import guru.springframework.controllers.PropertyInjectedController;
import guru.springframework.controllers.SetterInjectedController;
import guru.springframework.examplebeans.FakeDataSource;
import guru.springframework.examplebeans.FakeJmsBroker;
import guru.springframework.scope.PrototypeBean;
import guru.springframework.scope.SingletonBean;
import guru.springframework.services.GreetingService;
import guru.springframework.services.GreetingServiceFactory;
import guru.springframework.services.GreetingServiceImpl;
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());
FakeDataSource fakeDataSource = (FakeDataSource) ctx.getBean(FakeDataSource.class);

//System.out.println(fakeDataSource.getUser());

FakeJmsBroker fakeJmsBroker = (FakeJmsBroker) ctx.getBean(FakeJmsBroker.class);
//System.out.println(fakeJmsBroker.getUsername());

System.out.println("property injection:");
PropertyInjectedController propertyInjectedController=(PropertyInjectedController) ctx.getBean("propertyInjectedController");
System.out.println(propertyInjectedController.sayHello());

SetterInjectedController setterInjectedController = (SetterInjectedController) ctx.getBean("setterInjectedController");
System.out.println(setterInjectedController.sayHello());

GreetingFactoryController greetingFactoryController=(GreetingFactoryController) ctx.getBean("greetingFactoryController");
System.out.println(greetingFactoryController.sayHello());

System.out.println("dependecy injection :");
GreetingService greetingService=(GreetingServiceImpl) ctx.getBean("basePrimaryGreetingService");
System.out.println(greetingService.sayGreeting());

System.out.println("///SCOPE");
SingletonBean singletonBean1=ctx.getBean(SingletonBean.class);
System.out.println(singletonBean1.getMyScope());
SingletonBean singletonBean2=ctx.getBean(SingletonBean.class);
System.out.println(singletonBean2.getMyScope());

PrototypeBean prototypeBean1=ctx.getBean(PrototypeBean.class);
System.out.println(prototypeBean1.getMyScope());
PrototypeBean prototypeBean2=ctx.getBean(PrototypeBean.class);
System.out.println(prototypeBean2.getMyScope());

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

import guru.springframework.repository.GreetingRepository;
import guru.springframework.services.GreetingService;
import guru.springframework.services.GreetingServiceFactory;
import guru.springframework.services.GreetingServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Configuration
public class GreetingServiceConfig {
/**
* Her bir dile ait greeting service'leri burada ayağa kaldırıyorum.
* Bunun için önce bir service factory sınıfından bean yaratıyorum.
* daha sonra bu bean'in createService methodu ile her dile uygun bir service yaratıyorum.
* @param repository
* @return
*/

@Bean
GreetingServiceFactory greetingServiceFactory(GreetingRepository repository){
System.out.println("this repo works");
return new GreetingServiceFactory(repository);
}

@Bean
@Primary
@Profile({"default", "en"})
GreetingService primaryGreetingService(GreetingServiceFactory greetingServiceFactory){
return greetingServiceFactory.createGreetingService("en");
}

@Bean
@Primary
@Profile("es")
GreetingService primarySpanishGreetingService(GreetingServiceFactory greetingServiceFactory){
return greetingServiceFactory.createGreetingService("es");
}

@Bean
@Primary
@Profile("de")
GreetingService primaryGermanGreetingService(GreetingServiceFactory greetingServiceFactory){
return greetingServiceFactory.createGreetingService("de");
}

//repoyu kendisi bağlıyor annotation ile
@Bean(name = "basePrimaryGreetingService")
GreetingService basePrimaryGreetingService(GreetingRepository repository){
return new GreetingServiceImpl(repository);
}
}
63 changes: 63 additions & 0 deletions src/main/java/guru/springframework/config/PropertyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package guru.springframework.config;

import guru.springframework.examplebeans.FakeDataSource;
import guru.springframework.examplebeans.FakeJmsBroker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;

@Configuration
//@PropertySource({"classpath:datasource.properties", "classpath:jms.properties"})
public class PropertyConfig {


@Value("${guru.username}")
String user;

@Value("${guru.password}")
String password;

@Value("${guru.dburl}")
String url;

@Value("${guru.jms.username}")
String jmsUsername;

@Value("${guru.jms.password}")
String jmsPassoword;

@Value("${guru.jms.url}")
String jmsUrl;

@Bean
public FakeDataSource fakeDataSource(){
FakeDataSource fakeDataSource = new FakeDataSource();
fakeDataSource.setUser(user);
fakeDataSource.setPassword(password);
fakeDataSource.setUrl(url);
return fakeDataSource;
}

@Bean
public FakeJmsBroker fakeJmsBroker(){
FakeJmsBroker jmsBroker = new FakeJmsBroker();
jmsBroker.setUsername(jmsUsername);
jmsBroker.setPassword(jmsPassoword);
jmsBroker.setUrl(jmsUrl);
return jmsBroker;
}

/*
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =new PropertySourcesPlaceholderConfigurer();
return propertySourcesPlaceholderConfigurer;
}

*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package guru.springframework.controllers;

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

@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,19 @@
package guru.springframework.controllers;

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

@Controller
public class GreetingFactoryController {

private final GreetingService greetingService;

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

public String sayHello(){
return greetingService.sayGreeting();
}
}
20 changes: 20 additions & 0 deletions src/main/java/guru/springframework/controllers/MyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package guru.springframework.controllers;

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

@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,20 @@
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;


@Controller
public class PropertyInjectedController {

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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;

@Controller
public class SetterInjectedController {
private GreetingService greetingService;

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

@Autowired
public void setGreetingService(@Qualifier("setterGreetingService") GreetingService greetingService) {
this.greetingService = greetingService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package guru.springframework.examplebeans;

public class FakeDataSource {
private String user;
private String password;
private String url;

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
Loading