Giriş
Şu satırı dahil ederiz.
Şöyledir
Ayrıca bu arayüzden kalıtan bazı sınıflar şöyle
ClassPathXmlApplicationContext
ConfigurableApplicationContext
Erişim
Örnek - ApplicationContextAware
Şöyle yaparız
Bu sınıfa erişebilmek için Spring Bean'lerinde şöyle yaparız.
Şöyle yaparız.
Açıklaması şöyle
+  ApplicationContext
Açıklaması şöyle
Elimizde Spring tarafından yaratılmayan ve içinde Autowired olan bir sınıf olsun.
Eğer bean register edilmemişse NoSuchBeanException fırlatabilir. Açıklaması şöyle
Şöyle yaparız.
Şöyle yaparız.
Spring tarafından yüklenen tüm bean'lerin dizisinid döndürür.
Örnek
Şöyle yaparız.
Döngü ile şöyle yaparız.
Döngü ile şöyle yaparız.
Elimizde şöyle bir xml olsun.
1. <context:annotation-config>
Autowiring için. Yani bean'leri inject etmek için. XML'de ref şeklinde bean'leri bağlamaya gerek kalmaz. Şöyle yaparız.
Autodiscovery için. Şöyle yaparız.
@Component, @Service, @Repository, @Controller, @Endpoint
@Configuration, @Bean, @Lazy, @Scope, @Order, @Primary, @Profile, @DependsOn, @Import, @ImportResource
3. <tx:annotation-driven>
Şöyle yaparız.
Şöyle yaparız.
Şu satırı dahil ederiz.
import org.springframework.context.ApplicationContext;The ApplicationContext is where your Spring beans live.Kalıtım
Şöyledir
Ayrıca bu arayüzden kalıtan bazı sınıflar şöyle
ClassPathXmlApplicationContext
ConfigurableApplicationContext
Erişim
Örnek - ApplicationContextAware
Şöyle yaparız
@SpringBootApplication
public class DummyApplication implements ApplicationContextAware {
  public static void main(String[] args) {
    SpringApplication.run(DummyApplication.class, args);
  }
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) {
    System.out.println(applicationContext.getClass().getName());
  }
}Bu sınıfa erişebilmek için Spring Bean'lerinde şöyle yaparız.
@Autowired
private ApplicationContext appContext;WebApplicationContext appContext = WebApplicationContextUtils
  .getWebApplicationContext(getServletContext());Şöyle yaparız.
ApplicationContext context = new AnnotationConfigApplicationContext(
  ConfigurationProvider.class);@Configuration
public class ConfigurationProvider {
  ...
}Açıklaması şöyle
FileSystemXmlApplicationContext can access all your file system, for example c:/config/applicationContext.xml.Şöyle yaparız.
ApplicationContext context = new FileSystemXmlApplicationContext
  ("/config/beans.xml");Açıklaması şöyle
1. An ApplicationContext cannot have more than 1 parent ApplicationContext.Şöyle yaparız.
2. When a given ApplicationContext cannot resolve a bean, it will pass on the resolution request to its parent.
3. The parent of an ApplicationContext is specified in its constructor.
ApplicationContext ctx1 = new ClassPathXmlApplicationContext("context1.xml");
String[] configPath = new String[1];
configPath[0] = "context2.xml";
ApplicationContext ctx2 = new ClassPathXmlApplicationContext(configPath,ctx1);Elimizde Spring tarafından yaratılmayan ve içinde Autowired olan bir sınıf olsun.
public class Foo {
  @Autowired
  private ServiceJob serviceJob;
  ...
}Foo foo = ...;
applicationContext.getAutowireCapableBeanFactory().autowireBean(foo);applicationContext.getAutowireCapableBeanFactory().initializeBean(foo, null);BeanDefinitionRegistry factory = (BeanDefinitionRegistry)
  context.getAutowireCapableBeanFactory();
factory.removeBeanDefinition("MyBean");Eğer bean register edilmemişse NoSuchBeanException fırlatabilir. Açıklaması şöyle
AyrıcaException thrown when a BeanFactory is asked for a bean instance for which it cannot find a definition. This may point to a non-existing bean, a non-unique bean, or a manually registered singleton instance without an associated bean definition.
Şöyle yaparız.
Foo f = context.getBean(Foo.class);Şöyle yaparız.
Foo f = (Foo) context.getBean("Foo");Spring tarafından yüklenen tüm bean'lerin dizisinid döndürür.
Örnek
Şöyle yaparız.
public void printBeans() {
  System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames()));
}Döngü ile şöyle yaparız.
ApplicationContext applicationContext = ...;
for (String name: applicationContext.getBeanDefinitionNames()) {
  System.out.println(name);
}Döngü ile şöyle yaparız.
String[] beans = appContext.getBeanDefinitionNames();
Arrays.sort(beans);
for (String bean : beans) {
  System.out.println(bean);
}Elimizde şöyle bir xml olsun.
<bean id="voterId" class="com.spring.test2.Student" primary="true">
  <property name="name" value="hello"/>
  <property name="number" value="2080"/>
</bean>applicationContext.getBeansOfType(Student.class).get("voterId")
getBeansWithAnnotation metodu - class
Örnek
Şöyle yaparız.
Açıklaması şöyle.
XML  - Yeni kullanımÖrnek
Şöyle yaparız.
Map<String,Object> beans = applicationContext.getBeansWithAnnotation(Foo.class);Açıklaması şöyle.
Before the actual singleton bean instances are created, and your @PostConstruct method is called, the bean factory reads all the available configurations and registers all the encountered bean definitions.Şöyle yaparız.
getBeansWithAnnotation() should find a Foo bean, if it wasn't created from it's bean definition before, it will be created when you request it in @PostConstrust.
@Component
@DependsOn("testBean")
@CustomAnnotation
public class Foo {
}
@Service("testBean")
public class TestBean {
   @Autoware
   private Application context;
   @PostContruct
   public void init() {
      context.getBeansWithAnnotation(CustomAnnotation.class);
   }
}1. <context:annotation-config>
Autowiring için. Yani bean'leri inject etmek için. XML'de ref şeklinde bean'leri bağlamaya gerek kalmaz. Şöyle yaparız.
<context:annotation-config /> Autodiscovery için. Şöyle yaparız.
<context:component-scan base-package="org.package"/> @Component, @Service, @Repository, @Controller, @Endpoint
@Configuration, @Bean, @Lazy, @Scope, @Order, @Primary, @Profile, @DependsOn, @Import, @ImportResource
3. <tx:annotation-driven>
Şöyle yaparız.
<beans...>
  <context:component-scan base-package="..."/>
  <tx:annotation-driven/>
  ...
</beans>Şöyle yaparız.
<beans ...>
  <mvc:resources mapping="/resources/**"
                 location="/resources/"/>
  <mvc:annotation-driven/>
  ...
</beans>
 
Hiç yorum yok:
Yorum Gönder