Spring基础知识

本文主要包括以下内容

  1. 注解

注解

1、注解就是为了说明java中的某一个部分的作用(Type)
2、注解都可以用于哪个部门是@Target注解起的作用
3、注解可以标注在ElementType枚举类所指定的位置上
4、

         @Documented    //该注解是否出现在帮助文档中         @Retention(RetentionPolicy.RUNTIME) //该注解在java,class和运行时都起作用               @Target(ElementType.ANNOTATION_TYPE)//该注解只能用于注解上                  public @interface Target {            ElementType[] value();                    }    

5、用来解析注解的类成为注解解析器

自定义注解实例

类注解

package cn.itcast.annotation.sh;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Description {String value();
}

方法注解

package cn.itcast.annotation.sh;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodDesc {String name();
}

注解的使用

package cn.itcast.annotation.sh;@Description("whuhan university")
public class ITCAST_SH {@MethodDesc(name="whu")public void java(){System.out.println("this is java");}
}

注解解析

package cn.itcast.annotation.sh;import java.lang.reflect.Method;import org.junit.Test;public class AnnotationParse {@Testpublic void testParse(){Class class1 = ITCAST_SH.class;if(class1.isAnnotationPresent(Description.class)){Description description = (Description)class1.getAnnotation(Description.class);if(description.value().contains("whuhan")){System.out.println("学费减半");}}Method[] methods = class1.getMethods();for(Method method:methods){if(method.isAnnotationPresent(MethodDesc.class)){MethodDesc methodDesc = (MethodDesc)method.getAnnotation(MethodDesc.class);if(methodDesc.name().contains("whu")){System.out.println("折上折");}}}}
}

@Resource注解的使用(依赖注入)

1、在spring的配置文件中导入命名空间

         http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

2、引入注解解析器

        <context:annotation-config></context:annotation-config>

3、在spring的配置文件中把bean引入进来
4、在一个类的属性上加

            @Resource(name="student_annotation")private Student student;从该注解本身@Target({TYPE, FIELD, METHOD})@Retention(RUNTIME)public @interface Resource {String name() default "";}

1、该注解可以用于属性上或者方法上,但是一般用户属性上
2、该注解有一个属性name,默认值为””
5、分析整个过程:
1、当启动spring容器的时候,spring容器加载了配置文件
2、在spring配置文件中,只要遇到bean的配置,就会为该bean创建对象
3、在纳入spring容器的范围内查找所有的bean,看哪些bean的属性或者方法上加有@Resource
4、找到@Resource注解以后,判断该注解name的属性是否为”“(name没有写)
如果没有写name属性,则会让属性的名称的值和spring中ID的值做匹配,如果匹配成功则赋值
如果匹配不成功,则会按照类型进行匹配,如果匹配不成功,则报错
如果有name属性,则会按照name属性的值和spring的bean中ID进行匹配,匹配成功,则赋值,不成功则报错

实例

package cn.itcast.spring.annotation.di;public class Student {public void show(){System.out.println("student");}
}

Person.java

package cn.itcast.spring.annotation.di;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class Person {private Long pid;private String pname;@Resource(name="student_annotation")
//  @Autowired
//  @Qualifier("student")private Student student;private Set sets;private List lists;private Map map;private Properties properties;public void showStudent(){this.student.show();}
}

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- 注解解析器--><context:annotation-config></context:annotation-config><bean id="person_annotation" class="cn.itcast.spring.annotation.di.Person"></bean><bean id="student_annotation" class="cn.itcast.spring.annotation.di.Student"></bean>
</beans>

类扫描的注解Component使用

1、在spring的配置文件中导入命名空间

         http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

2、加入下面的语句

<context:component-scan base-package="cn.itcast.annotation.scan"></context:component-scan>
   1、 该注解解析器包含了两个功能:依赖注入和类扫描          2、在base-package包及子包下查找所有的类     

3、如果一个类上加了@Component注解,就会进行如下的法则

        如果其value属性的值为""          @Componentpublic class Person {}==<bean id="person" class="..Person">如果其value属性的值不为""@Component("p")public class Person {}==<bean id="p" class="..Person">

4、按照@Resource的法则再次进行操作

实例

package cn.itcast.annotation.scan;import org.springframework.stereotype.Component;@Component("b")
//<bean id="b" class="..Student">
public class Student {public void show(){System.out.println("student");}
}

person.java

package cn.itcast.annotation.scan;import javax.annotation.Resource;import org.springframework.stereotype.Component;@Component("a")
//<bean id="a" class="..Person">
public class Person {@Resource(name="b")private Student student;public void showStudent(){this.student.show();}
}

XML文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- 类扫描的注解解析器component 指的就是一个类base-package 在该包及子包中进行扫描--><context:component-scan base-package="cn.itcast.annotation.scan"></context:component-scan>
</beans>

新特性

Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说, 你能用@Component来注解你的组件类, 但如果用@Repository、@Service或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。 例如,这些典型化注解可以成为理想的切入点目标。当然,在Spring Framework以后的版本中,@Repository、@Service和 @Controller也许还能携带更多语义。如此一来,如果你正在考虑服务层中是该用 @Component还是@Service, 那@Service显然是更好的选择。同样的,就像前面说的那样, @Repository已经能在持久化层中进行异常转换时被作为标记使用了。

示例

PersonAction(控制层)

package cn.itcast.annotation.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Controller;@Controller("personAction")
public class PersonAction {@Resource(name="personService")private PersonService personService;public void savePerson(){this.personService.savePerson();}
}

PersonServiceImpl(service层)

package cn.itcast.annotation.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Service;@Service("personService")
public class PersonServiceImpl implements PersonService{@Resource(name="personDao")private PersonDao personDao;@Overridepublic void savePerson() {// TODO Auto-generated method stubthis.personDao.savePerson();}}

PersonDaoImpl(dao层)

package cn.itcast.annotation.mvc;import org.springframework.stereotype.Repository;@Repository("personDao")
public class PersonDaoImpl implements PersonDao{@Overridepublic void savePerson() {// TODO Auto-generated method stubSystem.out.println("save person dao");}}

xml与注解:

1、xml书写麻烦,但是效率高
2、注解书写简单,但是效率低

完成

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/494200.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

拥抱人工智能报告:中国未来就业的挑战与应对

来源&#xff1a; 199IT互联网数据中心近日&#xff0c;中国发展研究基金会联合红杉资本中国基金&#xff0c;对外发布了一份名为《投资人力资本&#xff0c;拥抱人工智能&#xff1a;中国未来就业的挑战与应对》的研究报告。在这篇报告中&#xff0c;研究课题组对比中外、调研…

《Python 快速入门》C站最全Python标准库总结

点赞 ➕ 评论 ➕ 收藏 养成三连好习惯 &#x1f345; 联系作者&#xff1a; 不吃西红柿 &#x1f345; 作者简介&#xff1a;CSDN 博客专家丨全站 Top 10&#x1f3c6;、HDZ 核心组成员、信息技术智库公号号主 &#x1f345; 简历模板、PPT 模板、学习资料、技术互助。关注…

工业4.0进行时:未来协作方式的变革

来源&#xff1a;资本实验室协作是将人类智力发挥至极致的方式&#xff0c;也是推动人类社会进步的重要手段。随着各种新技术的发展与应用&#xff0c;人类之间的协作方式也在随着技术的进步而进步。从面对面交流&#xff0c;到电话与传真、电子邮件与OA系统&#xff0c;再到在…

java之代理设计模式

代理模式是常用的java设计模式&#xff0c;他的特征是代理类与委托类有同样的接口&#xff0c;代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类&#xff0c;以及事后处理消息等。代理类与委托类之间通常会存在关联关系&#xff0c;一个代理类的对象与一个委托…

lisp中的*,**,***

在lisp中“*”除了乘法的作用外&#xff0c;还被用来保存REPL&#xff08;read-eval-print-loop&#xff09;中的返回值。其中 * -> 保存最后一次返回值。 ** -> *的上一次值。 *** -> **的上一次值。 例子如下&#xff1a; 而且  * (car /) ** …

《Python 快速入门》一千个程序员有一千套编码规范

一千个读者有一千个哈姆莱特。 -- 莎士比亚 一千个程序员有一千套编码规范。 -- 不吃西红柿 目录 1、分号 2、命名 3、行长度 4、缩进 5、空行 6、空格 7、类 8、块注释和行注释 9、字符串 10、导包 【总结】 1.命名 2.空白 3.语句 4.注释 Python 编码…

为了帮粉丝完成毕业设计,我发现了一款私活神器

一、缘起 不日前&#xff0c;有粉丝找到我&#xff0c;让我帮着做个&#xff1a; 教师管理系统 由于种种借口&#xff0c;我当时把问题交给群友去解决了..... 思来想去&#xff0c;越想越内疚&#xff0c;于是就请教了经常做私活的小伙伴。 必须分享给更多的小伙伴~ 二、揭开面…

狗脸识别APP整合

本文主要包括以下内容 android studio中导入so文件 通过URI获得Bitmap android studio中导入so文件 在main文件夹下建立jniLibs目录&#xff0c;并将so文件拷贝进去即可。 注意 声明的native方法与so文件中定义的方法的包名必须相同 通过URI获得Bitmap private Bitmap …

解析丰田对自动驾驶汽车的愿景:打造更加安全的汽车

丰田高管约翰莱昂纳德在丰田研究所的麻省理工学院车库&#xff0c;在他身后是研究所改造的一辆雷克萨斯选自&#xff1a;Bloomberg来源&#xff1a; 网易科技参与&#xff1a;乐邦约翰莱昂纳德(John Leonard)漫步走到麻省理工学院(MIT)校园里一间单调乏味的一层车库&#xff0c…

C#用IrisSkin4.dll 美化Winform窗体

前期准备&#xff1a; 1、IrisSkin4.dll2、Skin文件&#xff0c;后缀名为.ssk3、一个WinForm程序 直接拷贝IrisSkin4.dll文件到系统目录里&#xff1a;1、Windows 95/98/Me系统&#xff0c;将dll复制到C:\Windows\System目录下。2、Windows NT/2000系统&#xff0c;将dll复制到…

luogu题目精讲(C++):【入门1】顺序结构(1~5题)

B2002 Hello,World! # Hello,World! ## 题目描述 编写一个能够输出 Hello,World! 的程序。 提示&#xff1a; - 使用英文标点符号&#xff1b; - Hello,World! 逗号后面**没有**空格。 - H 和 W 为**大写**字母。 ## 输入格式 ## 输出格式 ## 样例 #1 ### 样例输入 #1 …

C站最全Python库总结丨标准库+高级库

梦想还是要有的&#xff0c;万一别人问呢&#xff1f; 作者&#xff1a;不吃西红柿 简介&#xff1a;CSDN博客专家、蓝桥签约作者、大数据&Python领域优质创作者。 CSDN私信我&#xff0c;回复【资料】领取&#xff1a; 1、100套小编购买的简历模板&#xff1b; 2、1000套…

postgresql命令

转自&#xff1a;http://blog.sina.com.cn/s/blog_4b93170a01000b2i.html1.PostgresSQL 支持标准的 SQL 类型 int&#xff0c;smallint&#xff0c; real&#xff0c;double precision&#xff0c; char(N)&#xff0c; varchar(N)&#xff0c;date&#xff0c; time&#xff0…

DeepMind-深度学习: AI革命及其前沿进展 (54页ppt报告)

来源&#xff1a;专知摘要&#xff1a;2018年9 月 9 日-14 日&#xff0c;DeepMind主办的Deep Learning Indaba 2018 大会在南非斯泰伦博斯举行。会上&#xff0c;牛津大学教授和其他15位专家做了《深度学习: AI革命及其前沿进展》的报告。Nando de FreitasNando de Freitas是一…

❤️ 6个Python办公黑科技,工作效率提升100倍!HR小姐姐都馋哭了(附代码)❤️

&#x1f345; 作者&#xff1a;不吃西红柿 &#x1f345; 简介&#xff1a;CSDN博客专家&#x1f3c6;、信息技术智库公号作者✌。简历模板、职场PPT模板、技术难题交流、面试套路尽管【关注】私聊我。 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有…

Spring之数据库操作

本文主要包括以下内容 springjdbc数据库操作 springjdbc声明事务处理 springhibernate声明事务处理 springjdbc数据库操作 方法 1、让自己写的一个dao类继承JdbcDaoSupport 2、让自己写的一个dao类继承JdbcTemplate 3、让自己写的一个dao类里有一个属性为JdbcTemplate …

iOS网络编程-ASIHTTPRequest框架同步请求

在ASIHTTPRequest框架中与HTTP请求相关的类有&#xff1a;ASIHTTPRequest和ASIFormDataRequest&#xff0c;其中最常用的是ASIHTTPRequest&#xff0c;ASIFormDataRequest是ASIHTTPRequest的子类&#xff0c;ASIFormDataRequest可以发送类似与HTML表单数据&#xff0c;也可以上…

苹果未来秘密在这里!从神秘组织到七大技术布局

来源&#xff1a;智东西随着人工智能的艰难发展&#xff0c;智能手机增长的放缓&#xff0c; 苹果公司能否第三次重塑自我&#xff1f;在很多方面&#xff0c;苹果仍然是一家以Steve Jobs个人形象制造的公司&#xff0c;专注于颠覆性产品。但今天&#xff0c;苹果走在了十字路口…

❤️ 爬虫分析CSDN大佬之间关系,堪比娱乐圈 ❤️

&#x1f345; 作者主页&#xff1a;不吃西红柿 &#x1f345; 简介&#xff1a;CSDN博客专家&#x1f3c6;、信息技术智库公号作者✌简历模板、PPT模板、技术资料尽管【关注】私聊我。历史文章目录&#xff1a;https://t.1yb.co/zHJo &#x1f345; 欢迎点赞 &#x1f44d; …

编程之美--读书笔记--返回一个数组中所有元素被第一个元素除的结果

笔试题目1&#xff1a;写一个函数&#xff0c;返回一个数组中所有元素被第一个元素除的结果 很多人会想到如下&#xff1a; void DivAarry&#xff08;int *pArray,int size) { for(int isize-1;i>0;i--) { pArray[i] / pArray[0]; } } 问题1&#xff1a;可不可以把循环正着…