Skip to content

Commit

Permalink
yasin.bhojawala@gmail.com
Browse files Browse the repository at this point in the history
Evaluation article on Different Types of Bean Injection in Spring
  • Loading branch information
yasin3061 committed Mar 1, 2017
1 parent 442c005 commit 963cc51
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
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 spring-core/src/main/java/com/baeldung/beaninjection/MainApp.java
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;
}
}
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;
}
}
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);
}
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);
}
}
15 changes: 15 additions & 0 deletions spring-core/src/main/resources/beaninjection.xml
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>

0 comments on commit 963cc51

Please sign in to comment.