JAVA基础开发概括

三大框架

  Spring

        简单的说spring主要功能是使用容器和依赖注入方便实例的管理,另外还提供了面向切面编程AOP、对事务管理、对象/关系映射、JavaBeans、JDBC、JMS 和其他技术的支持,极大的方便了开发

      依赖注入
        xml配置
@Data
public class MyController {

    private MyService myService;

    public MyController(MyService myService){
        this.myService = myService;
    }
}

public class MyService {

    public void doSomething() {
        System.out.println("Doing something...");
    }
}

applicationContext.xml(放在resources下)
<?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="myService" class="com.sky.example.MyService"></bean>

    <!--set方式注入、id是注入bean中的名字、class 是类名、property 是按照set方式注入 -->
    <bean id="myController" class="com.sky.example.MyController">
        <property name="myService" ref="myService"></property>
    </bean>
    <!--构造方法注入  constructor-arg 是按照构造方式注入
        index 是第几个参数 -->
    <bean id="myController" class="com.sky.example.MyController">
        <constructor-arg index="0" name="myService" ref="myService"></constructor-arg>
    </bean>


</beans>

@Test
    public void testBean(){
        //实例化一个容器
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 检索配置的实例
        MyController bean = classPathXmlApplicationContext.getBean(MyController.class);
        // 使用配置的实例
        System.out.println(bean.toString());
        bean.getMyService().doSomething();
    }

    基于注解配置(@Component + @ComponentScan)

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
}

public interface CategoryService {

}

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

}

        Spring Boot会自动扫描带有@Component@Service@Repository@Controller等注解的类,并将它们注册为Bean,然后通过使用@Autowired可以自动注入CategoryServiceImpl的实例,如果启动类没有使用@SpringbootApplication,就要使用 @ComponentScan才能扫描到并放入容器中,实际上@Service@Repository@Controller等注解都包含了@Comonent注解,所以这种方式就是@Component + @ComponentScan

      基于Java的配置(配置类)
/**
 * 配置类 用于创建AliOssUtil对象
 */
@Configuration
@Slf4j
public class OssConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
        log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties);
        return new AliOssUtil(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId()
                , aliOssProperties.getAccessKeySecret(), aliOssProperties.getBucketName());
    }
}

SpringMVC

springMVC是一种架构,它是Spring 框架
中处理模型(Model)- 视图(View)- 控制器(Controller)或 MVC 模式的一个模块,

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值