From 4afcff940551079617e8f4116e52bb0ef9df7fcc Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 25 Apr 2026 21:53:33 +0530 Subject: Added Spring Framework sample code --- .../04-basic-no-xml-java-configuration/.gitignore | 9 ++++ .../04-basic-no-xml-java-configuration/README | 12 ++++++ .../04-basic-no-xml-java-configuration/pom.xml | 16 +++++++ .../src/main/java/com/example/spring/App.java | 50 ++++++++++++++++++++++ .../src/main/java/com/example/spring/Maths.java | 19 ++++++++ .../main/java/com/example/spring/MathsUser.java | 16 +++++++ 6 files changed, 122 insertions(+) create mode 100644 spring-framework/04-basic-no-xml-java-configuration/.gitignore create mode 100644 spring-framework/04-basic-no-xml-java-configuration/README create mode 100644 spring-framework/04-basic-no-xml-java-configuration/pom.xml create mode 100644 spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/App.java create mode 100644 spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/Maths.java create mode 100644 spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/MathsUser.java (limited to 'spring-framework/04-basic-no-xml-java-configuration') diff --git a/spring-framework/04-basic-no-xml-java-configuration/.gitignore b/spring-framework/04-basic-no-xml-java-configuration/.gitignore new file mode 100644 index 0000000..ec4e05e --- /dev/null +++ b/spring-framework/04-basic-no-xml-java-configuration/.gitignore @@ -0,0 +1,9 @@ +# Eclipse +bin +.settings +.metadata +.classpath +.project + +# Maven +target diff --git a/spring-framework/04-basic-no-xml-java-configuration/README b/spring-framework/04-basic-no-xml-java-configuration/README new file mode 100644 index 0000000..5e8720d --- /dev/null +++ b/spring-framework/04-basic-no-xml-java-configuration/README @@ -0,0 +1,12 @@ +This project illustrates how a Java class can be made to describe the Spring configuration +where the objects returned by methods in the class are made to be the Spring beans. + +The class is annotated with @Configuration which indicates that the primary purpose of the +class is to act as a source of bean definitions. + +@Bean is used with a method to indicate that the method instantiates, configures, and +initializes a new object to be managed by the Spring IoC container. It plays the same role +as the element. + +It is not necessary for the classes from which beans are instantiated to have annotations +like @Component or @Autowired since the @Bean annotated methods perform the configuration. diff --git a/spring-framework/04-basic-no-xml-java-configuration/pom.xml b/spring-framework/04-basic-no-xml-java-configuration/pom.xml new file mode 100644 index 0000000..b76e1f0 --- /dev/null +++ b/spring-framework/04-basic-no-xml-java-configuration/pom.xml @@ -0,0 +1,16 @@ + + 4.0.0 + + + com.example.spring + base-config + 0.0.1-SNAPSHOT + ../00-config + + + basic-no-xml-java-configuration + jar + + diff --git a/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/App.java b/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/App.java new file mode 100644 index 0000000..1e613c6 --- /dev/null +++ b/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/App.java @@ -0,0 +1,50 @@ +package com.example.spring; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class App { + + public static void main(String[] args) { + @SuppressWarnings("resource") + ApplicationContext ctx = new AnnotationConfigApplicationContext(App.class); + + MathsUser mathsUser = ctx.getBean(MathsUser.class); + + mathsUser.doTask(); + } + + @Bean + public Maths maths() { + Maths maths = new Maths(); + maths.setB(100); + + return maths; + } + + @Bean + public MathsUser mathsUser() { + MathsUser mathsUser = new MathsUser(); + + /* + * What would the maths() return? Would it be the Spring managed bean or what + * would be returned when the maths() method is directly invoked? + * + * @Bean can be used with classes annotated with @Component tool. + * + * In a class annotated with @Configuration, maths() return the Spring managed bean. + * + * In a class annotated with @Component, what is returned by maths() is not the + * Spring managed bean, but what would be returned when the maths() method is directly invoked. + * + * For more details, look at the 'Full @Configuration vs “lite” @Bean mode?' description + * found in the 'Core Technologies' section of the Spring Framework Reference documentation. + */ + mathsUser.setMaths(maths()); + + return mathsUser; + } +} \ No newline at end of file diff --git a/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/Maths.java b/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/Maths.java new file mode 100644 index 0000000..b42d32d --- /dev/null +++ b/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/Maths.java @@ -0,0 +1,19 @@ +package com.example.spring; + +public class Maths { + + private int b; + + public int getB() { + return b; + } + + public void setB(int b) { + this.b = b; + } + + + public int add(int a) { + return a + b; + } +} diff --git a/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/MathsUser.java b/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/MathsUser.java new file mode 100644 index 0000000..aba1836 --- /dev/null +++ b/spring-framework/04-basic-no-xml-java-configuration/src/main/java/com/example/spring/MathsUser.java @@ -0,0 +1,16 @@ +package com.example.spring; + +public class MathsUser { + + private Maths maths; + + public void setMaths(Maths maths) { + this.maths = maths; + } + + public void doTask() { + int result = maths.add(10); + + System.out.println(result); + } +} -- cgit v1.2.3