BAEL-2850: Difference between <context:annotation-config> vs <context:component-scan> (#7291)
* BAEL-2850: Difference between <context:annotation-config> vs <context:component-scan> * BAEL-2850: Difference between <context:annotation-config> vs <context:component-scan>
This commit is contained in:
parent
9f5986963c
commit
8791269ec7
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.components;
|
||||
package com.baeldung.annotationconfigvscomponentscan.components;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.components;
|
||||
package com.baeldung.annotationconfigvscomponentscan.components;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
|
@ -4,11 +4,11 @@
|
|||
|
||||
<context:annotation-config />
|
||||
|
||||
<context:component-scan base-package="com.baeldung.components" />
|
||||
<context:component-scan base-package="com.baeldung.annotationconfigvscomponentscan.components" />
|
||||
|
||||
<bean id="accountService" class="com.baeldung.components.AccountService"></bean>
|
||||
<bean id="accountService" class="com.baeldung.annotationconfigvscomponentscan.components.AccountService"></bean>
|
||||
|
||||
<bean id="userService" class="com.baeldung.components.UserService">
|
||||
<bean id="userService" class="com.baeldung.annotationconfigvscomponentscan.components.UserService">
|
||||
<!-- If we have <context:annotation-config/> on top, then we don't need to set the properties(dependencies) in XML. -->
|
||||
<!-- <property name="accountService" ref="accountService"></property> -->
|
||||
</bean>
|
|
@ -5,14 +5,14 @@ import org.junit.Test;
|
|||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.baeldung.components.AccountService;
|
||||
import com.baeldung.components.UserService;
|
||||
import com.baeldung.annotationconfigvscomponentscan.components.AccountService;
|
||||
import com.baeldung.annotationconfigvscomponentscan.components.UserService;
|
||||
|
||||
public class SpringXMLConfigurationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenContextAnnotationConfigOrContextComponentScan_whenDependenciesAndBeansAnnotated_thenNoXMLNeeded() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:annotationconfigvscomponentscan-beans.xml");
|
||||
UserService userService = context.getBean(UserService.class);
|
||||
AccountService accountService = context.getBean(AccountService.class);
|
||||
Assert.assertNotNull(userService);
|
||||
|
|
Loading…
Reference in New Issue