要先配置一个注解模板spring-context-2.5.xsd
复制最后一个:
http://www.springframework.org/schema/context/spring-context-2.5.xsd
Eclipse里window - preferences - XML catalog - add 输入spring-context-2.5.xsd路径 - key type改第二个 - key里粘贴http://www.springframework.org/schema/context/spring-context-2.5.xsd
在上面手册里复制:xmlns:context="http://www.springframework.org/schema/context" 到xml文件抬头中去,再把手册中的:
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
复制进去,就有提示了。(这些在手册里都是黑体加粗)
配置完context是这样的:
通过注解实现IOC
鸡的类:
package cn.java.annotationioc; import org.springframework.stereotype.Component; @Component("smallJi") //就等于在xml中写了一个<bean id="smallJi" class="路径"> public class JI { public void behavior() { System.out.println("打鸣"); } }
鸡的xml:
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置注解驱动与包扫描 --> <!-- context:component-scan:组件扫描 (前置通知后置通知每一部分都是一个组件) base-package:会扫描包下所有java类中带有@Component的都会交给spring管理 --> <context:component-scan base-package="cn.java.annotationioc"></context:component-scan> </beans>
主方法:
package cn.java.annotationioc; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Window { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); JI ji = (JI) context.getBean("smallJi"); ji.behavior(); } }
设置单例多例:
package cn.java.annotationioc; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("smallJi") //就等于在xml中写了一个<bean id="smallJi" class="路径"> @Scope("singleton") //设置了单例,value设置了prototype就是多例 public class JI { public void behavior() { System.out.println("打鸣"); } }
通过注解实现DI
通过主方法调用Ji类为Duck类中方法赋值:
Duck.xml
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置注解驱动与包扫描 --> <!-- context:component-scan:组件扫描 (前置通知后置通知每一部分都是一个组件) base-package:会扫描包下所有java类中带有@Component的都会交给spring管理 --> <context:component-scan base-package="cn.java.annotationioc"></context:component-scan> </beans>
Duck.java
package cn.java.annotationioc; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("ya")//在bean中创建ya public class Duck { private String duckName; private Integer duckAge; public String getDuckName() { return duckName; } public void setDuckName(String duckName) { this.duckName = duckName; } public Integer getDuckAge() { return duckAge; } public void setDuckAge(Integer duckAge) { this.duckAge = duckAge; } @Override public String toString() { return "Duck [duckName=" + duckName + ", duckAge=" + duckAge + "]"; } }
Ji.java
package cn.java.annotationioc; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component("smallJi") //在bean中创建smallJi public class Ji { //@Resource(name="ya") //与Duck类保持一致 @Autowired //自动捆绑到Duck类 private Duck duck; public void behavior() { duck.setDuckName("唐老鸭"); duck.setDuckAge(20); System.out.println(duck); } }
主方法:
package cn.java.annotationioc; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Window { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Ji ji = (Ji) context.getBean("smallJi"); ji.behavior(); } }