实际开发的使用

实际开发中会将程序分为3层:

  1. Controller
  2. Servlet
  3. Repository(DAO)

关系Controller 调运Servlet 调运 Repository(DAO)

@Component 注解是将标注的类加载到IoC容器中,实际开发中可以分别根据

@Controller 控制层

@Service 业务层

@Repository 持久层

代码:

package com.southwind.Repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public interface myRepository {
    public  String domyRepository(Double score);
}

package com.southwind.Repository.impl;

import com.southwind.Repository.myRepository;
import org.springframework.stereotype.Repository;

@Repository
public class mymyRepositoryImpl  implements myRepository {
    @Override
    public String domyRepository(Double score) {
        String result="";
        if(score<60){
            result="不及格";
        }else if(score>=60&&score<80){
            result="合格";
        }else {
            result="优秀";
        }
        return result;
    }
}
package com.southwind.Service;

import org.springframework.stereotype.Component;


public interface myService {
    public String doSrvice(Double score);
}

package com.southwind.Service.impl;

import com.southwind.Repository.myRepository;
import com.southwind.Service.myService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Setter
@Service
public class myServiceImpl implements myService {
    @Autowired
    private myRepository  myRepository;
    @Override
    public String doSrvice( Double score) {
        return myRepository.domyRepository(score);
    }
}
package com.southwind.Controller;

import com.southwind.Service.myService;
import lombok.Data;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller(value = "a")
@Data
public class myControlller {
//客户端请求
    @Autowired
    private com.southwind.Service.myService myService;
;    public String service(Double score){
        return myService.doSrvice(score);
    }
}

配置:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--    <bean id="repository" class="com.southwind.entity.Repository"></bean>-->
<!--    自动扫包-->
    <context:component-scan base-package="com.southwind"></context:component-scan>
<!--    <bean id="controller" class="com.southwind.Controller.myControlller">-->
<!--        <property name="myService" ref="service"></property>-->
<!--    </bean>-->
<!--    <bean id="service" class="com.southwind.Service.impl.myServiceImpl">-->
<!--        <property name="myRepository" ref="repository"></property>-->
<!--    </bean>-->
<!--    <bean id="repository" class="com.southwind.Repository.impl.mymyRepositoryImpl"></bean>-->
</beans>

测试类:

package com.southwind.test;

import com.southwind.Controller.myControlller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test9 {
    public static void main(String[] args) {
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring-annotation.xml");
//        String[] s= applicationContext.getBeanDefinitionNames();
//        for(String name :s){
//            System.out.println(name);
//        }
//        客户端请求
        myControlller myControlller =(myControlller) applicationContext.getBean("a") ;
        String result = myControlller.service(new Double(77));
        System.out.println(result);
    }
}

Spring IoC的底层实现:

核心技术: XML解析+反射

具体思路:

  1. 根据需求编写XML文件,配置需要的创建的Bean.
  2. 编写程序需要的XML文件,获取Bean的相关信息,类,属性,id
  3. 根据第二步骤获得到的信息,结合反射机制动态的创建对象,同时完成属性的赋值
  4. 将创建好的bean存入Map集合中,设置key就是bean的id的值,value就是bean的对象
  5. 提供方法从Map中获得对应的value
posted on 2022-06-23 21:15  Steam残酷  阅读(64)  评论(0)    收藏  举报