Evaluation article on Different Types of Bean Injection in Spring
This commit is contained in:
@@ -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: ";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.beaninjection.domain;
|
||||
|
||||
public interface Logger {
|
||||
|
||||
public void logMessage(String message, int maxChars);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
15
spring-core/src/main/resources/beaninjection.xml
Normal file
15
spring-core/src/main/resources/beaninjection.xml
Normal file
@@ -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>
|
||||
Reference in New Issue
Block a user