IOC
- IOC即控制反转,使用IOC我们不用再一一创建对象来调用对象实例,这个创建的任务就交给了IOC容器来完成。下面我写了一些例子
- 在main方法里创建IOC容器,顺便输出一些注入内容:
- AplicationContext
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ManagerTest {
public static void main(String[] arg) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
User u=(User)ctx.getBean("first");
}
}
- 依赖注入(注入实例)
- Setter注入:
1、创建JavaBean
如:
package Test;
public class User {
private String name;
private String sex;
public User() {
super();
System.out.print("Ok");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2、配置文件:
<bean name="user" class="com.mr.User ">
<property name="name">
<value>老王</value>
</property>
....
</bean>
构造器注入:
1.JavaBean需要提供有参的构造方法
2.配置文件就不用l了,使用
<constructor-arg>
<value></value>
</constructor-arg>
引用别的Bean:
直接上code:
package Test;
public class FirstTest {
private String name;
private String sex;
private First_ f_;
public FirstTest() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "FirstTest [name=" + name + ", sex=" + sex + ", f_=" + f_ + "]";
}
public First_ getF_() {
return f_;
}
public void setF_(First_ f_) {
this.f_ = f_;
}
}
另一个JavaBean:
package Test;
public class First_ {
private String job;
public String getName() {
return job;
}
public void setName(String job) {
this.job = job;
}
public First_() {
super();
}
@Override
public String toString() {
return "First_ [job=" + job + "]";
}
}
关键一步,配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="first" class="Test.FirstTest">
<property name="name" >
<value>蝙蝠侠</value>
</property>
<property name="sex">
<value>男</value>
</property>
<property name="f_" ref="f_2"></property><!-- 主要是这里,这个name的值要跟第一个JavaBean的对象名一样 -->
</bean>
<bean id="f_2" class="Test.First_">
<property name="name">
<value>超级英雄</value>
</property>
</bean>
</beans>
再写一个main方法输出看效果:
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ManagerTest {
public static void main(String[] arg) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
FirstTest f=(FirstTest)ctx.getBean("first");
First_ f_=(First_)ctx.getBean("f_2");
System.out.print(f.toString());
}
}
输出图:
自动装配
- 按Bean名称:
只要在容器中存在某个bean,它的id即名称跟我的属性名称相同,就可以匹配:
举个栗子,show code:
–目标java bean
package Test;
public class User {
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
我自己的java bean:
package Test;
public class Printfo {
private User user;//注意这里的属性,它和User类的Bean名称相同
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void PrintUser() {
System.out.println("打印属性值如下所示:");
System.out.println(user.getName());
System.out.println(user.getSex());
System.out.println(user.getAge());
}
}
设置配文件:
<bean id="user" class="Test.User"><!--这里必须设置为user-->
<property name="name">
<value>刘备</value>
</property>
<property name="sex">
<value>男</value>
</property>
<property name="age">
<value>50</value>
</property>
</bean>
<bean autowire="byName" id="printfo" class="Test.Printfo"/>
main方法输出看看:
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Printfo pfo=(Printfo)ctx.getBean("printfo");
pfo.PrintUser();
}
}
运行图片:
–Aop:
Aop是面向切面编程,比如,有时候我们想没当我们运行一个方法就输出一个日记“xxx方法运行无误‘,如果,你的大工程几十个类文件里面都有差不多的方法,你不可能每个方法之后都加一个日记输出语句,所以需要AOP切入其中统一处理:
下面是一个AOP常用的方式之一,基于xml配置文件来进行切面编程。要求在TargetSubjext类里面的execute1方法输出前先输出一个日记:
(先导入这几个jar包)
TargetSubject:
package AOP;
public class TargetSubject {
public void execute1() {
System.out.println(1);
System.out.println(2);
System.out.println(3);
}
public void execute2() {
System.out.println("一");
System.out.println("二");
System.out.println("三");
}
}
接下来是我们的日记,专业叫通知:
Myadvice.java:
package AOP;
public class MyAdvice {
public void log() {
System.out.println("日记输出:");
}
}
之后重点是配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="advice">
<!-- 配置切入点,这里是前置通知,方法运行前执行,即需要做处理的那个方法 -->
<aop:before method="log" pointcut="execution(public void AOP.TargetSubject.execute1())"/>
</aop:aspect>
</aop:config>
<bean id="target" class="AOP.TargetSubject"></bean>
<bean id="advice" class="AOP.MyAdvice"></bean>
</beans>
最后主类测试一下
package AOP;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MangerAOp {
public static void main(String[] args) {
// TODO Auto-generated method stub
//加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Object bean=ctx.getBean("target");
TargetSubject to=(TargetSubject)bean;
to.execute1();//执行那个切入点,这里就会先调用日记方法
}
}