spring思维导图
2024-06-07 11:16:34   9  举报             
     
         
 spring思维导图
    作者其他创作
 大纲/内容
 @Controllerpublic class UserController {    @Autowired//通过声明的类名自动在Bean容器找到实例对象并引入,    //解决了层层间耦合,灵活引入userService实例对象及其对应的方法    @Qualifier(\"在实例化时@respository中更换的名字\")        private UserService userService;    public void show(){        //需要调用业务层的show方法        String show = userService.show();        System.out.println(show);    }}
  AOP:加强行为。通过切面表达式定位目标方法,使用@Before等注解执行前置后置、环绕等加强
  public interface UserService {    public String show();}@Servicepublic class UserServiceImpl implements UserService {    @Override    public String show() {        return \"userService\";    }}@Controllerpublic class UserController {    @Resource//=@AutoWired+@Qualifier    private UserService userService;    @Value(\"张三\")//直接给UserController类注入普通数据,并赋值(不要setter方法)    private String name;    public void show(){        //需要调用业务层的show方法        String show = userService.show();        System.out.println(show);    }}
  @Component@Aspectpublic class LogAspect {    //新建一个LogAspect类来定义切面和处理方法,@Aspect描述一个切面类    @Before(\"execution(* com.realzhao.UserService..*.*(..))\")//前置通知:在指定方法上添加    //切点表达式:execution()为表达主体,com.atguigu.UserService是要拦截的包名,    //后面两个点表示当前包和所有子包;*表示所有类;*(..)表示所有方法,不限参数    public void enhance(){        System.out.println(\"我是一段增强方法\");    }    @AfterReturning(\"execution(* com.realzhao.UserService.logout(..))\
  public class You implements RentHouse {    @Override    public void rentHouse() {        System.out.println(\"我要租房子!!!!!!\");    }}public class Me implements Marry {    @Override    public void marry() {        System.out.println(\"我要结婚了!!!!!\
  注解方式:@component,@repository,@service,@controller用于实例化对象,不需要再XML文件中再写bean了,最后只需要将要配置的包声明在XML文件中就行
  依赖注入:将实例对象注入到另一个实例对象中,使用构造器和set注入两种方式,但对于循环依赖问题,只能使用set注入
  注解方式:@Autowired和@Resource
  public class SpringIoCTest {    //如何获取方法    @Test    public void getBeanFromIoC(){        /*        ClassPathXmlApplicationContext 读取类路径下的xml文件即编译后的classes        FileSystemXmlApplicationContext        AnnotationConfigApplicationContext  读取配置类方式的IoC容器        WebApplicationContext  读取web项目         */        //得到Spring上下文路径,记得到Bean容器(其中已自动实例化要配置的类)        ClassPathXmlApplicationContext applicationContext1 = new ClassPathXmlApplicationContext(\"spring-03.xml\");                //方式一:通过id获取指定的bean对象的class路径,并实例化对象自己通过强转转为想要的对象        HappyComponent happyComponent = (HappyComponent) applicationContext1.getBean(\"happyComponent\");        //方式二:根据beanid,并进行强转        HappyComponent happyComponent1 = applicationContext1.getBean(\"happyComponent\
  Spring的AOP
  原理:JDK动态代理
  @Servicepublic class UserService {//目标类    public void login(){//目标方法        System.out.println(\"系统正在进行身份验证\");    }    public void logout(){        System.out.println(\"系统正在退出\");    }}
  XML方式:在Resources中新建一个spring.xml,并使用Spring上下文路径通过指定类名、Beanid方式获取实例对象(默认单例)
  //定义用户行为public interface Marry {    public void marry();}public class Me implements Marry{    @Override    public void marry() {        System.out.println(\"我要结婚了!!!!!\");    }}public class MarryCompanyProxy implements Marry{    //准备目标对象    private Marry target;    //构造器注入能够结婚的东西,并使用给定实例的方法    public MarryCompanyProxy(Marry target) {        this.target = target;    }    @Override    public void marry() {        target.marry();//调用目标角色方法        //同时增强用户行为        before();        after();    }    private void before() {        System.out.println(\"正在布置场地.......\");    }    private void after() {        System.out.println(\"新婚快乐,百年好合......\");    }}public class ProxyTest {    public static void main(String[] args) {        //目标对象        Me me = new Me();        //代理对象        MarryCompanyProxy marryCompanyProxy=new MarryCompanyProxy(me);        marryCompanyProxy.marry();    }}
  为此Spring提供了控制反转和依赖注入
  问题:在三层架构中,原始的方法引入方式是直接在上一层new一个下一层的实例,这导致层层之间耦合严重。若DAO层想要更换数据库连接,不仅需要调整DAO层代码,还需要调整Service层。违反OCP原则(对扩展开放,对修改关闭):耦合度较高,若想更新Dao层查询软件需要重写Service层    违反DIP依赖倒置原则(上层功能的实现,要不断依赖下层,下层改变就会导致上层一起改变)
  @Component//Spring会自动检测这个类,并将其实例化成一个Bean,//id为commmonComponent,class为当前类所在位置public class CommonComponent {}@Controllerpublic class XxxController {}@Repositorypublic class XxxDao {}@Servicepublic class XxxService {}        
  SpringJDBC:提供了在Spring框架中连接数据库的方法
  XML方式:
              bean id=\"userService1\" class=\"com.realzhao.ioc_02.UserService\
  控制反转:将对象的创建和生命周期管理交由外部容器(如Spring容器)来控制
  //只负责数据库增删改查,不负责连接数据库,需要配合Druid连接池public class JdbcTemplateTest {    public void testForJava(){        //创建Druid连接池:连接的创建和数据库注册        DruidDataSource dataSource=new DruidDataSource();        dataSource.setUrl(\"jdbc:mysql://127.0.0.1:3306/studb\");        dataSource.setDriverClassName(\"com.mysql.cj.jdbc.Driver\");        dataSource.setUsername(\"root\");        dataSource.setPassword(\"123456\");        //实例化对象:简化增删改查,但没有连接池        JdbcTemplate jdbcTemplate = new JdbcTemplate();        jdbcTemplate.setDataSource(dataSource);//dataSource连接池对象    }    //调用方法:数据库中增删改查操作    @Test    public void test(){        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext();        applicationContext.setConfigLocations(\"spring-01.xml\");        applicationContext.refresh();        JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean(\"jdbcTemplate\");//        String sql=\
  public class SpringIocTest {    @Test    public void Test(){        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(\"spring-01.xml\");        XxxDao bean = applicationContext.getBean(XxxDao.class);//通过xxxDao获取        System.out.println(bean);//com.atguigu.ioc_01.XxxDao@5852c06f        XxxService bean1 = (XxxService) applicationContext.getBean(\"xxxService\");//通过小写类名获取        System.out.println(bean1);//com.realzhao.ioc_01.XxxService@4149c063    }}
    
    收藏 
     
 
 
 
 
  0 条评论
 下一页
  
  
  
  
  
  
  
  
  
 