Spring-注解控件介绍

1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
下面写这个是引入component的扫描组件 
<context:component-scan base-package=”com.mmnc”>    
其中base-package为需要扫描的包(含所有子包) 
       1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 
   
@Service 
public class UserServiceImpl implements UserService { } 
@Repository 
public class UserDaoImpl implements UserDao { } 

 

getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)               
这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) 
@Scope(“prototype”)来改变
。可以使用以下方式指定初始化方法和销毁方法(方法名任意):
@PostConstruct 
public void init() { } 

 

转载于:https://www.cnblogs.com/hwaggLee/p/5121593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Spring MVC进行文件上传时,需要注意以下几点: 1. 首先,在Spring MVC配置文件中启用multipart解析器。可以通过在servlet-context.xml或applicationContext.xml文件中添加以下配置来完成: ```xml <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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <context:component-scan base-package="com.example" /> <mvc:annotation-driven /> <!-- 配置multipart解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10000000"/> </bean> </beans> ``` 这里我们使用了CommonsMultipartResolver类来处理文件上传,还可以通过maxUploadSize属性设置上传文件的最大大小。 2. 在Controller中编写处理文件上传的方法,并使用@RequestParam注释来接收上传的文件。例如: ```java @RequestMapping(value="/upload", method=RequestMethod.POST) public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // 文件上传逻辑处理 } catch (Exception e) { e.printStackTrace(); } } return "redirect:/successUrl"; } ``` 在上面的代码中,我们使用@RequestParam注释将上传的文件绑定到MultipartFile对象中,并使用getBytes()方法获取文件内容。 3. 最后,在表单中添加文件上传控件,并指定form的enctype属性为“multipart/form-data”。例如: ```html <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传" /> </form> ``` 这样就完成了文件上传的全部过程。注意,在实际应用中,还需要对上传的文件进行安全检查,例如检查文件类型、文件大小等,以确保应用的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值