Spring的注解开发

一、原始注解

UserDaoImpl类

复制代码
package com.Itheima.Dao.impl;

import com.Itheima.Dao.UserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


//   <bean id="userDao" class="com.Itheima.Dao.impl.userDaoImpl">   </bean>
@Repository("userDao")
public class UserDaoImpl implements UserDao {

//    private dateSource

    @Override
    public void save() {
        System.out.println(" save running.... ");
    }
    
}

ServiceImpl类

复制代码
package com.Itheima.Service.Impl;

import com.Itheima.Dao.UserDao;
import com.Itheima.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//  <bean id="userService" class="com.Itheima.Service.Impl.userServiceImpl">
//        <property name="userDao" ref="userDao"></property>
//    </bean>

@Service("userService")
@Scope("singleton")
public class UserServiceImpl implements UserService {

//    @Autowired  //按照数据类型从spring容器中进行匹配的
//    @Qualifier("userDao")//按照id的值从容器中进行匹配 此处我们的qualifier注解要结合autowired一起使用
    @Resource(name="userDao")//相当于Qulifier+autowired
    private UserDao userDao;

//如果使用的是xml配置的方式  set方法需要写  如果使用的是注解开发 set方法可以不写
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }

    @Override
    public void save() {
        userDao.save();
    }

    @PostConstruct
    public void init(){
        System.out.println("Service对象的初始化方法 " + userDao);
    }

    @PreDestroy
    public void destory(){
        System.out.println(" Service对象的销毁方法 ");
    }
}

UserController类

复制代码
package com.Itheima.web;

import com.Itheima.Service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserController {
    public static void main(String[] args) {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserService userService =(UserService) app.getBean("userService");
//        userService.save();
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService =(UserService) app.getBean("userService");
        userService.save();
        app.close();
    }
}

applicationContext.xml

复制代码
<?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
                http://www.springframework.org/schema/context/spring-context.xsd">
    <import resource="classpath:spring-mvc.xml"/>
<!--    配置组件扫描-->
    <context:component-scan base-package="com.Itheima"/>



<!--    加载外部的properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 在这里配置bean -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

<!--    <bean id="userDao" class="com.Itheima.Dao.impl.userDaoImpl">   </bean>-->

<!--    <bean id="userService" class="com.Itheima.Service.Impl.userServiceImpl">-->
<!--        <property name="userDao" ref="userDao"></property>-->
<!--    </bean>-->
</beans>

二、 新注解

SpringConfigurtion 主配置文件类

复制代码
package com.Itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;


//该注解表示这个类是Spring的核心配置文件类
@Configuration

//<context:component-scan base-package="com.Itheima"/>
@ComponentScan("com.Itheima")

//<import resource="classpath:spring-mvc.xml"/>加载分的配置文件
@Import({DataSourceConfiguration.class,XXX.class})//本质是一个数组

public class SpringConfigurtion{
   

}

DataSourceConfiguration类 数据源相关的配置类

复制代码
package com.Itheima.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

//相当于 <!--    加载外部的properties文件-->
//    <context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")

public class DataSourceConfiguration {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")//spring会将当前方法的返回值以指定名称存储到spring容器当中
    public DataSource getDataSource()throws Exception{
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass(driver);
        ds.setJdbcUrl(url);
        ds.setUser(username);
        ds.setPassword(password);
        return ds;
    }
}

package com.Itheima.web;

import com.Itheima.Service.UserService;
import com.Itheima.config.SpringConfigurtion;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserController {
    public static void main(String[] args) {
        
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfigurtion.class);
        UserService userService =(UserService) app.getBean("userService");
        userService.save();

    }
}
相关推荐
武子康9 分钟前
Embedding 替换怎么评测:把索引成本算进成功任务
人工智能·后端·agent
LMT流氓兔19 分钟前
第五单元——第9课:防止同一次操作重复执行
java·服务器·数据库·算法·ai·langchain
OxYGC28 分钟前
Spring 面经系列 · 第 6 篇:Spring MVC 请求全流程、父子容器与零配置原理
java·spring·mvc
code2cat31 分钟前
Java高级篇之JDBC:连接数据库,完成查询、更新与事务
java·开发语言·数据库
bksczm36 分钟前
Redis数据结构之string
java·redis·mybatis
cc_瀚海知行39 分钟前
资料检索 1|直接放入全文、按需读文件和先检索,有什么区别?
java·人工智能·ai
BioRunYiXue1 小时前
使用docker分析高通量测序数据
java·数据库·人工智能·spring cloud·docker·tomcat·html
小尹哥-程序员1 小时前
第9集:Spring AI Advisor链:像Spring MVC拦截器一样优雅
人工智能·spring·mvc
Shaoshing1 小时前
缓存一致问题及常见解决方案
java·缓存
聚美智数2 小时前
文档转换 API‑文档转 PDF‑PDF 转文档‑异步文档处理‑文件格式迁移‑转换结果回调推送 API 接口介绍
android·java·pdf