示例:一般方法调用类,耦合性太强
package com.jt.demo1;
public interface Pet {
void hello();
}
package com.jt.demo1;
public class Cat implements Pet{
@Override
public void hello() {
System.out.println("狗拿耗子多管闲事!!!");
}
}
package com.jt.demo1;
public class Dog implements Pet{
public void hello(){
System.out.println("虽然我不是人,但你是真的狗!!!");
}
}
package com.jt.demo1;
public class User {
private static Pet pet = new Dog();
public static void main(String[] args) {
pet.hello();
}
}
示例:反射解耦合
package com.jt.demo2;
public interface Pet {
void hello();
}
package com.jt.demo2;
public class Cat implements Pet {
@Override
public void hello() {
System.out.println("狗拿耗子多管闲事!!!");
}
}
package com.jt.demo2;
public class Dog implements Pet {
public void hello(){
System.out.println("虽然我不是人,但你是真的狗!!!");
}
}
package com.jt.demo2;
//本类用于测试反射
public class NewDog {
/*1.该方法是通过反射的机制实例化对象!!框架中的重点!!!
* 2.反射机制:
* java中创建对象常用的一种方式,指定类型的路径
* 之后通过关键方法.newInstancs()实例化对象
* 类比:xxx.newInstance()~~ new Dog();
* 3.精髓:为什么用反射而不是new?
* 3.1 因为第三方通过反射实例化对象,可以实现松耦合!!
* 3.2 一般多用于框架,因为框架不清楚用户到底需要啥,只有运行期才清楚用户
* 到底需要什么对象,扩展性更好!!!
* 4.异常说明
* 1.运行时异常 运行期间出错了
* 2.编译异常/检查异常 编译时提示报错
* 5.反射时,必须调用对象的无参构造方法!!!*/
public static void main(String[] args) throws Exception{
Dog dog = (Dog) Class.forName("com.jt.demo2.Dog").newInstance();
dog.hello();
}
}
package com.jt.demo2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class User {
public static void main(String[] args) {
String resource = "spring.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(resource);
Dog dog = context.getBean(Dog.class);
dog.hello();
Cat cat = context.getBean(Cat.class);
cat.hello();
}
}
示例:Spring框架解耦合
package com.jt.demo3;
public interface Pet {
void hello();
}
package com.jt.demo3;
public class Dog implements Pet {
public void hello(){
System.out.println("虽然我不是人,但你是真的狗!!!");
}
}
package com.jt.demo3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //标识当前类是配置类
@ComponentScan("com.jt.demo3") //包扫描注解:让Spring注解生效!!!
public class SpringConfig {
//作用:和配置文件类似,管理对象!!!
/**
* IOC-写法
* 1.@Bean 告诉Spring容器,当前方法的名称是Map中的key
* 返回值是Map中的value
* 2.特殊用法:
* 常规条件下,Spring通过反射实例化对象
* 也可以由用户自己new的方式创建
* @return
*/
@Bean
public Dog dog(){
return new Dog();
}
}
package com.jt.demo3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class User {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConfig.class);
Dog dog = context.getBean(Dog.class);
dog.hello();
}
}
示例:demo4
package com.jt.demo4;
public interface Pet {
void hello();
}
package com.jt.demo4;
public class Cat implements Pet {
@Override
public void hello() {
System.out.println("狗拿耗子多管闲事!!!");
}
public Cat(){
System.out.println("波斯猫眯着它的双眼!!!");
}
}
package com.jt.demo4;
public class Dog implements Pet {
public void hello(){
System.out.println("虽然我不是人,但你是真的狗!!!");
}
public Dog(){
System.out.println("我左边坐着个二哈!");
}
}
package com.jt.demo4;
import org.springframework.context.annotation.*;
@Configuration //标识当前类是配置类
@ComponentScan("com.jt.demo4") //包扫描注解:让Spring注解生效!!!
public class SpringConfig {
//作用:和配置文件类似,管理对象!!!
/**
* IOC-写法
* 1.@Bean 告诉Spring容器,当前方法的名称是Map中的key
* 返回值是Map中的value
* 2.特殊用法:
* 常规条件下,Spring通过反射实例化对象
* 也可以由用户自己new的方式创建
* @return
*/
public SpringConfig(){
System.out.println("我是这个容器的无参构造");
}
@Bean //默认单例
// @Scope("singleton") //默认单例
// @Bean
// public Cat cat(){ return new Cat(); }
@Scope("prototype") //多例对象
// @Lazy //开启懒加载
public Dog dog(){
return new Dog();
}
}
package com.jt.demo4;
//该测试类用于测试单例和多例模式/懒加载机制
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class User {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConfig.class);
context.getBean(Dog.class);
context.getBean(Dog.class);
context.getBean(Dog.class);
}
}
示例:demo5
package com.jt.demo5;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**Spring自动为该注解标识的类通过反射实例化对象,交给Spring容器管理
* key:类名首字母小写user
* value:反射的对象
* 类似于@Bean注解
*
* @Component/@Bean区别:
* 1.@Component Spring容器通过反射自动创建对象
* @Bean 是用户自己手动创建对象
* 2.@Component 标识类的
* @Bean 标识配置类中的方法
* 3.@Component 对象的Id是类名,首字母小写
* @Bean 对象的Id是方法名
*/
@Component
public class User {
//1.对象的创建
public User(){
System.out.println("用户创建对象成功!!!");
}
//2.进行初始化操作
@PostConstruct
public void init(){
System.out.println("为属性赋值");
}
//3.进行业务调用 业务方法需用户手动调用
public void hello(){
System.out.println("我爱学java");
}
//4.销毁方法
@PreDestroy
public void destory(){
System.out.println("调用销毁方法,释放资源!");
}
}
package com.jt.demo5;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.jt.demo5")
public class SpringConfig {
//自己手动创建对象,交给Spring容器管理.
// @Bean
// public User user(){
// return new User();
// }
}
package com.jt.demo5;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestUser {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(SpringConfig.class);
User user = (User)context.getBean("user");
user.hello();
//如果需要执行销毁方法,则需要先关闭容器对象
//思想:销毁动作是敏感行为,特殊处理 实现类中有关闭方法.
context.close();
}
}
示例:demo6
package com.jt.demo6;
public class Car {
private String color;
private String name;
public Car(){
System.out.println("我开车贼猛");
}
public Car(String color, String name){
this.color = color;
this.name = name;
System.out.println("我车速贼快");
}
public void go(){
System.out.println("让我们一起开着"+color+"的"+name+"逮虾户!!!");
}
}
package com.jt.demo6;
public class Person {
private Integer id;
private String name;
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void togo(){
car.go();
}
public void sayHi(){
System.out.println("你好:"+id+":"+name);
}
}
package com.jt.demo6;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPerson {
public static void main(String[] args) {
String resource = "spring_di.xml";
ApplicationContext context =
new ClassPathXmlApplicationContext(resource);
Person person = context.getBean(Person.class);
person.sayHi();
person.togo();
}
}