package entities;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2017/8/3 0003.
*/
@Component
public class Knight {
private Horse horse;
Knight(Horse horse){
this.horse = horse;
}
public void ride(){
horse.bark();
}
}
package entities;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2017/8/3 0003.
*/
@Component
public class Horse {
void bark(){
System.out.println("Horse!!!!");
}
}
package config;
import entities.Knight;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2017/8/3 0003.
*/
@Configuration
@ComponentScan(basePackageClasses = {Knight.class})
public class ActionConfig {
}
package entities;
/**
* Created by Administrator on 2017/8/3 0003.
*/
public class Knight {
private Horse horse;
public Knight(Horse horse){
this.horse = horse;
}
public void ride(){
horse.bark();
}
}
package entities;
/**
* Created by Administrator on 2017/8/3 0003.
*/
public class Horse {
void bark(){
System.out.println("Horse!!!!");
}
}
package config;
import entities.Horse;
import entities.Knight;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2017/8/3 0003.
*/
@Configuration
public class ActionConfig {
@Bean
public Knight getKnight(){
return new Knight(getHorse());
}
@Bean
public Horse getHorse(){
return new Horse();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="knight" class="entities.Knight">
<constructor-arg ref="horse"></constructor-arg>
</bean>
<bean id="horse" class="entities.Horse"></bean>
</beans>
package config;
import entities.Horse;
import entities.Knight;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Created by Administrator on 2017/8/3 0003.
*/
@Configuration
@Import(BeautyConfig.class)
public class ActionConfig {
@Bean
public Knight getKnight(){
return new Knight(getHorse());
}
@Bean(name = "horse")
public Horse getHorse(){
return new Horse();
}
}
package config;
import entities.Beauty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2017/8/3 0003.
*/
@Configuration
public class BeautyConfig {
@Bean
Beauty getBeauty(){
return new Beauty();
}
}
package main;
import config.ActionConfig;
import entities.Beauty;
import entities.Knight;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Administrator on 2017/8/3 0003.
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ActionConfig.class);
Knight knight = context.getBean(Knight.class);
knight.ride();
Beauty beauty = context.getBean(Beauty.class);
beauty.sleep();
}
}
@ImportResource("classpath:spring.xml")
<import resource="another.xml"></import>
<bean class="config.BeautyConfig"></bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="another.xml"></import>
<bean class="config.BeautyConfig"></bean>
<bean id="knight" class="entities.Knight">
<constructor-arg ref="horse"></constructor-arg>
</bean>
<bean id="horse" class="entities.Horse"></bean>
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有