lazy-init="true"懒加载:在此实例被用到的时候才会被实例化,而不会在spring的配置文件中直接被实例化
<!--直接自动扫描包,将bean注册到spring容器-->
<context:component-scan base-package="com.zrm"></context:component-scan>
<context:component-scan base-package="com.msb"></context:component-scan>
component-scan可以自动扫描包内容,并注册Bean到Spring容器
在需要注册到容器的类上添加@Component标签,标识这个类由Spring容器接管
在一个类上添加@Component默认会使用首字母小写的类名作为ID注册到Spring容器。
如果需要手动指定Bean Id可以使用@Component("p")
@Controller @Service @Repository
这三个注意在MVC开发中会经常用到,除了注解名字和Component不一样之外,其余功能都一样。
Spring额外提供这三个注解的目的主要是为了区分MVC中每个类的区别。
使用注解注册Bean 默认的作用域还是singleton,可以使用@Scope("prototype")改变对象作用域
在使用注解给对象注入值的时候,不再需要Get/Set方法
使用@Value注解
@Value("小明")
private String name;
@Autowired
private Pet MyPet;
使用@Autowired注解
默认是ByType的,如果需要ByName需要配合@Qualifier注解
@Autowired()
@Qualifier("p2")
private Pet MyPet;
实例:如下
//通过一个简单的MVC模式的实例并且使用注解的方式来实现的逻辑代码如下 //controller部分 package com.zrm.MainController; import com.zrm.MainEntity.Usre; import com.zrm.MainService.Service; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("conl") public class Controller { @Autowired private Usre usre; @Autowired private Service service; @Test public void out(){ System.out.println(this.usre); } public void control() { if (service.getUser(usre) == null) { System.out.println("找不到对应的信息"); } else { System.out.println("login successfully"); } } } service部分 package com.zrm.MainService; import com.zrm.MainDao.UserDao; import com.zrm.MainEntity.Usre; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Service { @Autowired private UserDao userDao; public Usre getUser(Usre usre) { System.out.println("通过controller的获取到的user去Dao查询的得到的User"); return userDao.getUser(usre); } } user部分 package com.zrm.MainEntity; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; /* * 通过注解方式注入值的时候不需要get,set方法 * */ @Component("user")//向spring容器中注册bean @Scope("prototype")//修改bean的类型默认为singleton public class Usre { @Value("18") private Integer age; @Value("zrm") private String name; } userdao部分 package com.zrm.MainDao; import com.zrm.MainEntity.Usre; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class UserDao { //@Autowired //自动注入,默认是bytype如果要通过byname要使用添加@Qualifier("user") public Usre getUser(Usre usre) { System.out.println("这里是UserDao获取的user"); return new Usre(); } } //启动类 package com.zrm; import com.zrm.MainController.Controller; import com.zrm.MainEntity.Usre; import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("contextApplication.xml"); Controller controller = ctx.getBean("conl", Controller.class); controller.control(); Usre user = ctx.getBean("user", Usre.class); System.out.println(ToStringBuilder.reflectionToString(user, ToStringBuilder.getDefaultStyle())); } }
原文:https://www.cnblogs.com/zrmj/p/11482122.html