Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit f59848f

Browse files
committedOct 11, 2018
Add bean overriding example
1 parent 6f7f090 commit f59848f

File tree

9 files changed

+98
-0
lines changed

9 files changed

+98
-0
lines changed
 

‎bean-overriding/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-framework-tutorial</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>bean-overriding</artifactId>
13+
14+
15+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.bobocode;
2+
3+
4+
import com.bobocode.configs.MainConfig;
5+
import com.bobocode.service.TalkingService;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
9+
public class BeanOverridingExample {
10+
public static void main(String[] args) {
11+
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
12+
13+
TalkingService talkingService = context.getBean(TalkingService.class);
14+
System.out.println(talkingService.saySomething());
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.bobocode.configs;
2+
3+
import com.bobocode.service.TalkingService;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class AdditionalConfig {
9+
10+
@Bean("talkingService")
11+
public TalkingService pythonFanService() {
12+
return () -> "Python is awesome!";
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.bobocode.configs;
2+
3+
import com.bobocode.service.TalkingService;
4+
import org.springframework.context.annotation.*;
5+
6+
@Configuration
7+
@ComponentScan(basePackages = "com.bobocode.service")
8+
@ImportResource("classpath:application-context.xml")
9+
@Import(AdditionalConfig.class)
10+
public class MainConfig {
11+
12+
@Bean("talkingService")
13+
public TalkingService javaFanService() {
14+
return () -> "Java is the best!";
15+
}
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.bobocode.service;
2+
3+
4+
public interface TalkingService {
5+
String saySomething();
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.bobocode.service.impl;
2+
3+
import com.bobocode.service.TalkingService;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service("talkingService")
7+
public class CFanService implements TalkingService {
8+
@Override
9+
public String saySomething() {
10+
return "I like C, and I don't care...";
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.bobocode.service.impl;
2+
3+
4+
import com.bobocode.service.TalkingService;
5+
6+
public class PhpFanService implements TalkingService {
7+
8+
@Override
9+
public String saySomething() {
10+
return "Php is still good :D";
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
<bean id="talkingService" class="com.bobocode.service.impl.PhpFanService"/>
6+
</beans>

‎pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>mvc-basics</module>
1818
<module>rest-basics</module>
1919
<module>jpa-tx-management</module>
20+
<module>bean-overriding</module>
2021
</modules>
2122

2223
<properties>

0 commit comments

Comments
 (0)
This repository has been archived.