-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evaluation article on Different Types of Bean Injection in Spring
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
spring-core/src/main/java/com/baeldung/beaninjection/BeanConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.baeldung.beaninjection; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.baeldung.beaninjection.domain.ConsoleLogger; | ||
import com.baeldung.beaninjection.domain.Logger; | ||
|
||
@Configuration | ||
@ComponentScan("com.baeldung.beaninjection") | ||
public class BeanConfiguration { | ||
|
||
@Bean | ||
public Logger logger() { | ||
return new ConsoleLogger(); | ||
} | ||
|
||
@Bean | ||
public int maxChars() { | ||
return 15; | ||
} | ||
|
||
@Bean | ||
public String messagePrefix(){ | ||
return "JConfig: "; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spring-core/src/main/java/com/baeldung/beaninjection/MainApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.baeldung.beaninjection; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
import com.baeldung.beaninjection.domain.LoggingHelper; | ||
|
||
public class MainApp { | ||
public static void main(String[] args) { | ||
|
||
LoggingHelper logHelper = loadFromJavaConfig(); | ||
logHelper.logMessage("This is a log message"); | ||
|
||
logHelper = loadFromXmlConfig(); | ||
logHelper.logMessage("This is another log message"); | ||
} | ||
|
||
private static LoggingHelper loadFromJavaConfig() { | ||
ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class); | ||
|
||
LoggingHelper loggingHelper = context.getBean(LoggingHelper.class); | ||
((ConfigurableApplicationContext)context).close(); | ||
|
||
return loggingHelper; | ||
} | ||
|
||
private static LoggingHelper loadFromXmlConfig() { | ||
ApplicationContext context = new ClassPathXmlApplicationContext("beaninjection.xml"); | ||
|
||
LoggingHelper loggingHelper = context.getBean(LoggingHelper.class); | ||
((ConfigurableApplicationContext)context).close(); | ||
|
||
return loggingHelper; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
spring-core/src/main/java/com/baeldung/beaninjection/domain/ConsoleLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.baeldung.beaninjection.domain; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class ConsoleLogger implements Logger { | ||
|
||
private String messagePrefix; | ||
private static final String DEFAULT_MESSAGE_PREFIX = "LOG: "; | ||
|
||
@Override | ||
public void logMessage(String message, int maxChars) { | ||
System.out.println(getMessagePrefix() + message.substring(0, maxChars)); | ||
} | ||
|
||
private String getMessagePrefix(){ | ||
return this.messagePrefix == null ? DEFAULT_MESSAGE_PREFIX : messagePrefix; | ||
} | ||
|
||
@Autowired | ||
public void setMessagePrefix(String messagePrefix){ | ||
this.messagePrefix = messagePrefix; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
spring-core/src/main/java/com/baeldung/beaninjection/domain/Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.baeldung.beaninjection.domain; | ||
|
||
public interface Logger { | ||
|
||
public void logMessage(String message, int maxChars); | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-core/src/main/java/com/baeldung/beaninjection/domain/LoggingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.beaninjection.domain; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class LoggingHelper { | ||
private Logger logger; | ||
private int maxChars; | ||
|
||
@Autowired | ||
public LoggingHelper(Logger logger, int maxChars) { | ||
this.logger = logger; | ||
this.maxChars = maxChars; | ||
} | ||
|
||
public void logMessage(String message) { | ||
logger.logMessage(message, maxChars); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | ||
|
||
<bean id="logHelper" class="com.baeldung.beaninjection.domain.LoggingHelper"> | ||
<constructor-arg index="0" ref="logger"/> | ||
<constructor-arg index="1" type="int" value="20"/> | ||
</bean> | ||
|
||
<bean id="logger" class="com.baeldung.beaninjection.domain.ConsoleLogger"> | ||
<property name="messagePrefix" value="XMLConfig: "></property> | ||
</bean> | ||
|
||
</beans> |