This repository was archived by the owner on Jan 13, 2023. It is now read-only.
File tree 9 files changed +98
-0
lines changed 9 files changed +98
-0
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package com .bobocode .service ;
2
+
3
+
4
+ public interface TalkingService {
5
+ String saySomething ();
6
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change 17
17
<module >mvc-basics</module >
18
18
<module >rest-basics</module >
19
19
<module >jpa-tx-management</module >
20
+ <module >bean-overriding</module >
20
21
</modules >
21
22
22
23
<properties >
You can’t perform that action at this time.
0 commit comments