由买买提看人间百态

topics

全部话题 - 话题: autowired
1 (共1页)
s****y
发帖数: 503
1
我写了一个包装Ehcache的类EhcacheWrapper,加入多线程控制,代码如下
@service
public class EhcacheWrapper{
public Element get(String cacheName, String key){......}
public void put(String cacheName, String key, Object value){......}
//多线程控制,代码略
}
我在一个controller中自动装配EhcacheWrapper,比如
public class GetLabelServiceImpl{
@Autowired
private EhcacheWrapper ehcacheWrapper;
//代码略
}
我在其他controller中还能@Autowired EhcacheWrapper吗?
我觉得@Component默认的scope是singleton,只有一个实例。如果在两个类中都@
Autowired EhcacheWrapper,是不是两个类都调用同一个b... 阅读全帖

发帖数: 1
2
按照我的项目解析,Spring默认Bean的Scope都是Singleton,多个Controller调用的是
同一个Bean,如果多线程里的变量是线程安全的,那么这个Bean就是线程安全的。
举例:实际应用中碰到过如下问题:
页面一次点击,提交两个请求到Controller,对应到Dao中两个方法,Dao类Autowired注
入一个StringBuilder,然后两个方法中各自append SQL字符串,最后toString()时候
得到的字符串是混在一起的。
将Scope改成了prototype后可以解决问题。
写的比较乱,见谅。
z****n
发帖数: 1933
3
来自主题: Java版 - 请教一个spring的问题
定义一个接口
public interface DataAccessDao
实现
public class DataAccessDaoImpl implements DataAccessDao
单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/DataAccessTest.xml")
public class DataAccessTest
{
@Autowired
private DataAccessDaoImpl dataAccessDao;
spring配置

然后出错, org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean o... 阅读全帖
b***i
发帖数: 3043
4
来自主题: Java版 - Spring例子求解释
先问薪问题
Spring已经在openshift上运行了,我发现,重启后,信息就丢了。那么,这个例子是
不是没有真正的数据库?说是叫H2 databas。如何让Spring把数据填入数据库?我看到
如下代码,那么EntityManager em被wire到哪里去了?应该在哪里查找设置?
@Controller
@RequestMapping(value="/")
public class MemberController
{
@Autowired
private MemberDao memberDao;
@RequestMapping(method=RequestMethod.GET)
public String displaySortedMembers(Model model) {... }
@RequestMapping(method=RequestMethod.POST)
public String registerNewMember(@Valid @ModelAttribute("newMember")
Member n... 阅读全帖
b***i
发帖数: 3043
5
来自主题: Java版 - spring解决了什么问题?
问个autowire问题,是不是
@Autowired
UserDao userdao;


是不是,这个bean名字是userDao,就被生成到了userdao这个property那里?可是大小
写不完全对吧?
t*********e
发帖数: 630
6
来自主题: Java版 - Spring Framework magic
The simplest example to demonstrate dependency injection in Spring Framework
. The main method isn't related to mockMessageSercvice() in any way, and
there is no any configuration file defined in the project. The main method
is able to print out "Hello World". How does this magic occur? @
Configuration & @ComponentScan work together to do the trick. It takes time
to just develop a basic understanding of how this little example works for
those who are new to the spring framework field.
Class 1:
p... 阅读全帖
z****e
发帖数: 54598
7
来自主题: Java版 - Spring Framework magic
当你启动spring framework的时候
framework会扫描你所有的pkg,尤其是class
然后自动initiate&index所有的beans
这个时候,因为你的@Autowire被扫描到了
spring会自动initiate所有的beans
然后符合@Autowired条件的bean就那么一个
所以每次用到,都会自动inject进去
当然,如果有多个beans符合条件,你就需要定义了
所以spring的启动并不快没发现?
t*********e
发帖数: 630
8
来自主题: Java版 - Spring Framework magic
The simplest example to demonstrate dependency injection in Spring Framework
. The main method isn't related to mockMessageSercvice() in any way, and
there is no any configuration file defined in the project. The main method
is able to print out "Hello World". How does this magic occur? @
Configuration & @ComponentScan work together to do the trick. It takes time
to just develop a basic understanding of how this little example works for
those who are new to the spring framework field.
Class 1:
p... 阅读全帖
z****e
发帖数: 54598
9
来自主题: Java版 - Spring Framework magic
当你启动spring framework的时候
framework会扫描你所有的pkg,尤其是class
然后自动initiate&index所有的beans
这个时候,因为你的@Autowire被扫描到了
spring会自动initiate所有的beans
然后符合@Autowired条件的bean就那么一个
所以每次用到,都会自动inject进去
当然,如果有多个beans符合条件,你就需要定义了
所以spring的启动并不快没发现?
Y**G
发帖数: 1089
10
来自主题: Programming版 - 问个spring的问题
假定我自己定义了一个qualifier: Year用来描述汽车制造的年份
@Qualifier
public @interface Year {
int value();
}
如果我需要2010年生产的汽车,我可以用
...
@Autowired
@Year(2010)
private Car car;
...
我怎么可以表达“我要2010年以后生产的汽车”?
下面的写法不工作:
...
@Autowired
@Year(value>2010)
private Car car;
...
Y**G
发帖数: 1089
11
来自主题: Programming版 - 问个spring的问题
假定我自己定义了一个qualifier: Year用来描述汽车制造的年份
@Qualifier
public @interface Year {
int value();
}
如果我需要2010年生产的汽车,我可以用
...
@Autowired
@Year(2010)
private Car car;
...
我怎么可以表达“我要2010年以后生产的汽车”?
下面的写法不工作:
...
@Autowired
@Year(value>2010)
private Car car;
...
s*****r
发帖数: 43070
12
连autowire都不知道,还di如何实现,太高深。
就是基本的OO deign和OR mapping的问题,让写个entity class没有id,直接bypass。
z****e
发帖数: 54598
13
来自主题: JobHunting版 - 问一个java的函数调用问题
professional的写法
如果是core java
Solution s = SolutionBuilder.build();
如果是j2ee
@Autowire
Solution s;
@Inject
Solution s;
@Validate
Solution s;
等等等等,写法很多,看具体情况具体分析,跟使用的framework也有关系
c******a
发帖数: 789
14
说是这么说,spring mvc里autowire彻底解决了那篇blog里提到singleton容易出的所
有问题。
u***n
发帖数: 21026
15
终于网上可以autowired了,唉,还不如刷题呢
g*****g
发帖数: 34805
16
我觉得挺好的,大量的bean可以autowire,AOP相关的东西
简单很多,比如transaction.

等java class都deprecated. 新spring framework推荐使用annotation.没有深刻研究
,不过好像spring取消了很多inheritance.感觉上是比较简化了程序.
o**1
发帖数: 6383
17
来自主题: Java版 - 一个关于GWT的问题
为什么要用 spring mvc? 直接 gwt async service 就可以吧,把 spring service
autowire 进 gwt service 里面。
S****h
发帖数: 558
18
来自主题: Java版 - spring question
Spring has quite a lot of autowire built-in. It will automatically detect
the defined bean in the required interface or class and use it if there is
no ambiguity.
z****e
发帖数: 54598
19
你现在用spring还没有用@去autowire private变量?
z****e
发帖数: 54598
20
来自主题: Java版 - 请教一个spring的问题
autowire + qualifier
但是推荐使用
resource
g*****g
发帖数: 34805
21
来自主题: Java版 - 是不是spring mvc用的很少
Most frameworks used annotation to handle di these days.
Even if you don't use DI, it's not simpler
Compare
private OtherService otherService;
public MyService(OtherService otherSerivce) {
this.otherService = otherService;
}
to
@Autowired
private OtherService otherService;
I don't see how the former can be simpler.

hard
c******n
发帖数: 4965
22
来自主题: Java版 - what is really hard is testing
--- "@Component is like a global service that has namespace"
what is the namespace ?
if in 2 libraries, written by 2 people, both mark
@Component
public class AuthorA implements SomeInterface {
}
@Component
public class AuthorB implements SomeInterface {
}
now in my application code, I just want to grab an "author", and do
public class MyTestSuite {
@Autowired
SomeInterface blah;
...
}
here since AuthorA and AuthorB are both discovered as "@Component", they are
like global variables, and makes... 阅读全帖
i**w
发帖数: 883
23
来自主题: Java版 - spring解决了什么问题?
autowiring by type
c******3
发帖数: 296
24
来自主题: Java版 - spring解决了什么问题?
对@Autowired来说,bean名字不重要,type匹配就好了。
b***i
发帖数: 3043
25
来自主题: Java版 - spring解决了什么问题?
我还是不理解啊。比如, HomePageController类有个Autowired UserService
userService;
那么,spring是在生成这个类的对象的时候就自动生成了userService?
然后我如果UserServiceImpl 和UserServiceImpl2都实现了UserService,那么如何决定
HomePageController这里的userService是用哪个呢?
z****e
发帖数: 54598
26
拿di做例子
spring比较早做到了di
但是后来jee标准也要求了di
那么所有jee impl的server全部都有了di
那如果手头上拿到的是jboss的话
怎么di?一样的,跟spring一样
写一个beans.xml,放到classpath下面去
让jboss在启动时候能够找到这个xml,然后就能用了
跟spring的di一样用,区别就是语法上的区别
spring以前用@Autowired,其他di用@Inject
后来spring也支持了@Inject,所以通用了
这样就可以把在spring上的经验就可以移植到
jboss, weblogic, websphere……上去用
java所有的framework的理论都是通用的
aop也是一样,jboss也实现了aop,用法跟spring类似
aspect j是aop的先行者,所以aspect j的一些语法
会比一般的aop impl比如spring aop要复杂一点
但是基本上aspect, pointcut这些概念都是共通的,所以只要留意一下语法差异
以前的经验就能直接搬过来用,所以最后剩下的其实就是理论
具体的实践反而显... 阅读全帖
Y**G
发帖数: 1089
27
来自主题: Programming版 - spring annotation
also is @Inject better than @Autowired?
W***o
发帖数: 6519
28
来自主题: Programming版 - spring annotation
I think @Autowired is more Spring native than @Inject
Y**G
发帖数: 1089
29
来自主题: Programming版 - 问个spring的问题
我的前提条件是容器的配置是正确的,2010以后的汽车只有一辆,不会没有。autowire
不会出现问题。
o****e
发帖数: 44
30
来自主题: Programming版 - 问个spring的问题
要实现lz的想法办法有几个。没那么复杂,lz想多了。
这跟spring真没关系。lz你的year interface只有一个int 变量,你给“value>2010”
这个进去,算啥?必须是int才行啊。
你这个写法,就不是java了,是人类语言了。lol。
直接写多一个annotation最简单,
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @in... 阅读全帖
Y**G
发帖数: 1089
31
来自主题: Programming版 - 问个spring的问题
把Year放到Car里面去,那怎么用autowired呢?
Y**G
发帖数: 1089
32
来自主题: Programming版 - 问个spring的问题
我的前提条件是容器的配置是正确的,2010以后的汽车只有一辆,不会没有。autowire
不会出现问题。
o****e
发帖数: 44
33
来自主题: Programming版 - 问个spring的问题
要实现lz的想法办法有几个。没那么复杂,lz想多了。
这跟spring真没关系。lz你的year interface只有一个int 变量,你给“value>2010”
这个进去,算啥?必须是int才行啊。
你这个写法,就不是java了,是人类语言了。lol。
直接写多一个annotation最简单,
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @in... 阅读全帖
Y**G
发帖数: 1089
34
来自主题: Programming版 - 问个spring的问题
把Year放到Car里面去,那怎么用autowired呢?
z****e
发帖数: 54598
35
来自主题: Programming版 - spring di的project
对于同一台jvm上的spring用法几乎是一样的
无论是不是toy还是prod. code
如果分布式的话,spring就不管了,你自己要想办法处理跨机器的通信
比如用jms,或者web service,或者rmi
总有一种rpc适合你
如果你非要举出例子来
public class MyClass{
@Autowire
private UrClass urClass
public void doSth(...){
...
urClass.func(...);
...
}
}
基本上就这个模式
z****e
发帖数: 54598
36


spring就是典型的非jee的产品
spring是非标准化di
真正的标准化di是beans.xml
和@Inject这些
spring用@Autowire
这个annotation就是典型的非标准化annotation
spring甚至还没有guice标准
guice倒是遵循了jsr标准
z****e
发帖数: 54598
37


spring就是典型的非jee的产品
spring是非标准化di
真正的标准化di是beans.xml
和@Inject这些
spring用@Autowire
这个annotation就是典型的非标准化annotation
spring甚至还没有guice标准
guice倒是遵循了jsr标准
x***4
发帖数: 1815
38
来自主题: Programming版 - Scala,F#或haskell怎么用DI?
有没有类似spring autowire的做法?
h*********8
发帖数: 404
39
that is y i hate all these DI thing...
y dont you simply write your code to do whatever u need?
N*****m
发帖数: 42603
40
来自主题: Programming版 - spring boot有什么坑吗
autowired,boot之前好久就有了
这种大路货,有坑也容易解决
1 (共1页)