How to auto-update an object without Spring XML context file?

I have an interface with Component annotation and some classes that implemented it as follows:

 @Component public interface A { } public class B implements A { } public class C implements A { } 

In addition, I have a class with the Autowired variable as follows:

 public class Collector { @Autowired private Collection<A> objects; public Collection<A> getObjects() { return objects; } } 

My context file consists of the following definitions:

 <context:component-scan base-package="org.iust.ce.me"></context:component-scan> <bean id="objectCollector" class="org.iust.ce.me.Collector" autowire="byType"/> <bean id="b" class="org.iust.ce.me.B"></bean> <bean id="c" class="org.iust.ce.me.C"></bean> 

And in the main class, I have several codes:

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); B b = (B) context.getBean("b"); C c = (C) context.getBean("c"); Collector objectCollector = (Collector) context.getBean("objectCollector"); for (A object : objectCollector.getObjects()) { System.out.println(object); } 

Output:

 org.iust.ce.me.B@1142196 org.iust.ce.me.C@a9255c 

These codes work well, but for some reason I don't want to use the xml context file. In addition, I prefer to create objects using the new operator instead of using the getBean() method. However, since AutoWiring really good idea in programming, I don't want to lose it.

Now I have two questions !!

  • How can I AutoWire classes that implement the A interface without using the xml context file?
    Is this even possible?

  • when I change the scope bean from singlton to prototype as follows:

    <bean id="b" class="org.iust.ce.me.B" scope="prototype"></bean>

    and create some beans, only the bean that was created when the context was created is injected in the Autowired variable. Why?

Any help would be appreciated.

+4
source share
3 answers

You do not know the version of Spring that you are using. But for now, you can use @Configuration to replace .xml. Take a look at @ Configuration

Below is the code in the documentation

 @Configuration public class ServiceConfig { private @Autowired RepositoryConfig repositoryConfig; public @Bean TransferService transferService() { return new TransferServiceImpl(repositoryConfig.accountRepository()); } } @Configuration public interface RepositoryConfig { @Bean AccountRepository accountRepository(); } @Configuration public class DefaultRepositoryConfig implements RepositoryConfig { public @Bean AccountRepository accountRepository() { return new JdbcAccountRepository(...); } } @Configuration @Import({ServiceConfig.class, DefaultRepositoryConfig.class}) // import the concrete config! public class SystemTestConfig { public @Bean DataSource dataSource() { /* return DataSource */ } } public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class); TransferService transferService = ctx.getBean(TransferService.class); transferService.transfer(100.00, "A123", "C456"); } 
+9
source

If the managed classes were properly annotated, Spring can scan application files to get the necessary information without any xml or java configuration files.

 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.scan("com.something.something.etc"); context.refresh(); ...context.getBean("name_of_bean"); 

Note AnnotationConfigApplicationContext is created without any arguments. context.scan("..."); takes a string that tells Spring where to look. that is, packets will be scanned

  com.something.something.etc.one 
  com.comething.something.etc.two 
, and classes in those packages annotated with @Component , @Autowired , etc. will be installed and injected where necessary.

This approach does not seem to be documented.

+2
source

1- You need to write another class that will perform the operation. write @Component to class B and C.

 public static void main(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); InitClass initClass = (InitClass) context.getBean("initClass"); } public class InitClass{ @Autowired public B b; @Autowired public C c; } 

with this you will get B and C without using xml.

2- http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html Bean areas are described in detail here. If you always want a new object, you must use a prototype, but creating a new one will be done in different classes. In the same class, you must add a new link.

as

 public class InitClass{ @Autowired public A a1; @Autowired public A a2; } 
0
source

Source: https://habr.com/ru/post/1438407/


All Articles